I am using marshmallow 2.0.0rc2 to validate input for HTTP requests and load SQLAlchemy models in JSON for HTTP responses. And I came across 2 problems:
Firstly, when loading data from JSON with an HTTP PUT request, I want to fill in all the missing fields as None in order to correctly overwrite the data in SQLAlchemy. Now I am using the following code:
for name, field in schema.fields.iteritems(): if field.missing == ma.missing: schema.fields[name].missing = None
It works, but I assume it is being tapped since I was fiddling with the marshmallow.Field instance attached to the Schema class. And after removing the Schema instance, all the fields that we fixed will depend on the new missing ones, and not by default.
Secondly, when dumping data from SQLAlchemy to JSON, all missing fields are resolved as None, and JSON is populated with data {"key": null, } . This is an undesirable behavior, and I clear them when post_dump run.
@post_dump def clean_missing(self, data): for key in filter(lambda key: data[key] is None, data): data.pop(key) return data
Like the previous one, it works, but includes the creation of some BaseSchema class, which passes this logic to all the inherited classes.
I searched the documentation so far, and did not find the right way to change this behavior, that is, skip fields when resetting and filling in None fields when loading. Am I missing something, or marshmallow does not provide such functions?