-
-
Notifications
You must be signed in to change notification settings - Fork 628
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
fields.Method result should be able to be wrapped into other field types #513
Comments
The Method and Function fields are actually non-beloved offsprings here as they do not stand for a particular type but change the way you access data. Other field types actually expect data to be accessed via properties. |
This is simple enough to implement as a custom field. from marshmallow import fields
class TypedMethod(fields.Method):
def __init__(self, inner_field, *args, **kwargs):
self.inner_field = inner_field
super().__init__(*args, **kwargs)
def _deserialize(self, value, attr, data):
preprocessed = super()._deserialize(value, attr, data)
return self.inner_field.deserialize(preprocessed)
def _serialize(self, value, attr, obj):
preprocessed = super()._serialize(value, attr, obj)
return self.inner_field._serialize(preprocessed, attr, obj) Usage: import datetime as dt
from marshmallow import Schema, fields
class MySchema(Schema):
created_at = TypedMethod(fields.DateTime('%Y-%m-%dT%H:%M:%S%z'), serialize='_created_at')
def _created_at(self, obj):
return obj.creation_time
class Thing:
def __init__(self):
self.creation_time = dt.datetime.utcnow()
schema = MySchema()
obj = Thing()
data = schema.dump(obj).data
assert isinstance(data['created_at'], str)
assert data['created_at'] == obj.creation_time.strftime('%Y-%m-%dT%H:%M:%S%z') Closing this for now. I'm going to hold off on adding this to core, as the custom field above is a simple workaround. We can reopen if there is further interest in adding this to core. |
@sloria I think the main problem here that the field's schema and the data being returned on serialization are mixed up. So, thinking from the architectural point of view first we need to provide a schema, such as
or for a more complex cases
In such a case |
We could add parameters And likewise |
@lafrech It would be great! |
@lig you may open an issue for this, to see how the idea is received, and eventually work on a PR. |
@lafrech I believe, this issue is exactly about this |
Alright. Let's reopen this. |
Yes, this would be a nicer feature to have. My current workaround is to create one attribute for serialization and one for deserialization. Example:
|
I like this too. It would solve this: #1638 |
Any progress on this? |
This change is being discussed in #2039. In fact in #2039 (comment), I shared a revelation I just had, which happens to be pretty close to what I had written here in #513 (comment) six years ago. I may be a bit slow, but at least I'm consistent. |
fields.Method is pretty much always used to return a certain type of value. It doesn't allow to choose options for the return value though.
For example, if one designes a field which should return a generic datetime and format it using a given format string, one is forced to use fields.Method which is linked to a schema method which returns a string(!), not a datetime. Because it is now impossible to provide a format string for the datetime anywhere except of the method itself.
In other words, something like this:
would be much more convenient and reusable than this:
The text was updated successfully, but these errors were encountered: