If we have a field that deserializes to an Enum but serializes from an object with an enum attribute, will it be easier to let the dump_by attribute to be callable?
This will avoid the hassle of creating a Method field with custom serialization and deserialization.
For example,
If the API request body is:
{
"name": "Some user",
"role": "PARENT" // enum here
}
and the view builds a user object, where the role is an sqlalchemy enum field (assuming role.label is an enum), then it would need to serialize from role.label.
Right now even subclassing wont work since __init__ raises a validation error:
class FromObjectEnumField(EnumField):
def _serialize(self, value, attr, obj):
if callable(self.dump_by):
value = self.dump_by(value)
return super()._serialize(value, attr, obj)
# raises `ValueError: Invalid selection for load_by must be EnumField.VALUE or EnumField.NAME, got <function UserSchema.<lambda> at 0x10c8ce040>` on `__init__`
Forcing me to override init as well. Is there a better way to handle this?
marshmallow-enum==1.5.1
If we have a field that deserializes to an
Enumbut serializes from an object with an enum attribute, will it be easier to let thedump_byattribute to be callable?This will avoid the hassle of creating a
Methodfield with custom serialization and deserialization.For example,
If the API request body is:
{ "name": "Some user", "role": "PARENT" // enum here }and the view builds a
userobject, where theroleis an sqlalchemy enum field (assumingrole.labelis an enum), then it would need to serialize fromrole.label.Right now even subclassing wont work since
__init__raises a validation error:Forcing me to override init as well. Is there a better way to handle this?
marshmallow-enum==1.5.1