-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Method field #53
Comments
Hi. AFAIK, nothing has been done already. @touilleMan opened an issue (#38) about it, meaning he does not need it right now but anyone willing to do it is welcome to send a PR. I thought I'd need MethodField so I gave it a naive attempt uncommenting those fields in umongo code. I think I got the same error as you did and didn't try any further. Right now, I have two use cases:
I hope I'm making sense... I'm not sure I didn't have the time yet to give a try to this setter/getter callback feature so I didn't post here about it. |
This is pretty much the case i'm looking for. Since i'm using umongo objects and serialize them for my API i cannot find a way to achive this. Is the only way to redeclare my whole umongo object as a marshmallow schema and dump the umongo through it |
ah ok, thanks for this! This will totally fit my needs. Since all these other technics exist i'm not sure for what to use the method/function functionallity of marshmallow in any way. |
I'm not sure either. Maybe @touilleMan has something in mind. Should we close this issue as a duplicate of #38? |
yes, totally. thanks again! |
Hi, I tried a bit to implement MethodField in umongo. As @lafrech stated it's not as simple as I thought at first !
So I'm thinking Example: @instance.register
class User(Document):
birthday = DateTimeField(required=True)
age = IntField(required=True)
class UserNoAgeSchema(User.schema.as_marshmallow_schema):
class Meta:
fields = ('birthday', )
def load_user(payload):
ret = UserNoAgeSchema().load(payload)
if ret.errors:
# do something
ret.data['age'] = (datetime.utcnow() - ret['birthday']).days / 365
return User(**ret.data) Or with the document hooks: @instance.register
class User(Document):
birthday = DateTimeField(required=True)
age = IntField(required=True)
def pre_insert(self):
self._compute_age()
def pre_update(self):
self._compute_age()
def _compute_age(self):
self.age = (datetime.utcnow() - self.birthday).days / 365 |
I still think it would be nice to have a way to call the callback at get or set time. class User(Document):
birthday = DateTimeField(required=True, on_set=self._compute_age) # either here
age = IntField(required=True, on_get=self._compute_age) # or there
def _compute_age(self):
self.age = (datetime.utcnow() - self.birthday).days / 365 One nice thing with umongo is that validation occurs at set time, so the object is always valid. You don't have to wait until commit (or explicit validation) to check that. get/set callbacks would allow those dependencies between fields to be enforced anytime just as well. |
@lafrech this is an interesting feature, I should create an issue for it. My 0.2$ on this: Given an umongo document keeps internally the data in mongo world representation, it would be a bit cumbersome to add a check to do dynamic evaluation when retrieving a field (like you propose with |
I'm in the need for computed attributes on my model objects.
Looking at marshmallow it seems the most apropriate way to do this is by implementing the
Method
field like:Looking at umongo the method field is not implemented yet. For testing i tried subclassing a new Field-Type like
of course resulting in an error. Marshmallow seems not to have access to the method field of the subclass (at this position) since
self.parent
isn't the umongo object but the marshmallow schema object.What would be the right path to implement the Method field funcionality? Was there already done something in this direction or should i dig deeper and find a solution on how to pass the method call to marshmallow?
The text was updated successfully, but these errors were encountered: