You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classInteger(fields.Integer):
def_deserialize(self, value, *args, **kwargs):
value=valueor0returnsuper()._deserialize(value, *args, **kwargs)
classTest(Schema):
foo=Integer(allow_none=True)
Test().load({'foo': None}) # but it produces {"foo": None} instead of {"foo": 0}
When None (or missing) is encountered _deserialize() is never executed, because Field.deserialize() returns immediately. The current work around appears to require an understanding of undocumented parts of the Field implementation and extending deserialize() instead. The documentation should probably explain the _/de/serialize() and None/missing interaction.
A similar use case is having a Nested field supply the default values for the nested schema's fields when the parent field is None/missing. We may want to explore improving the default behavior in a future version, but for now providing a pattern for this in the docs would be helpful.
If you want to create a field that transforms
None
to a preset value, a custom field is required.Currently the documentation for "Creating A Field Class" states:
https://marshmallow.readthedocs.io/en/latest/custom_fields.html#creating-a-field-class
When
None
(ormissing
) is encountered_deserialize()
is never executed, becauseField.deserialize()
returns immediately. The current work around appears to require an understanding of undocumented parts of theField
implementation and extendingdeserialize()
instead. The documentation should probably explain the_/de/serialize()
and None/missing interaction.A similar use case is having a
Nested
field supply the default values for the nested schema's fields when the parent field isNone
/missing. We may want to explore improving the default behavior in a future version, but for now providing a pattern for this in the docs would be helpful.Related to #388
The text was updated successfully, but these errors were encountered: