marshmallow: skip_missing is gone, how to skip None elements ?
I saw that v2.0.0b removed the skip_missing option, claiming it skips the missing entries by default.
However in my case (using mongoengine’s Document) a field is never missing: it returns None which is, strictly speaking, something…
I can see from the commit (https://github.com/marshmallow-code/marshmallow/commit/a3908d36967ac30764ceb66c52c99357425fbdec) that previous behavior of skipping empty values (like None or empty list/tuple) has been obliterated. Is there now a new way to do this ?
I’m thinking for now to overload the _serialize method of each field to check for the empty value and return the missing singleton in such a case… but this sound really cumbersome to me, there must be a better way !
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 21 (8 by maintainers)
Sorry for the delayed response on this.
How about using a post-dump method to remove any null values? Something like:
Howdy! Thanks @sloria for this. Though you will run into problems if you have dicts or anything not hashable. Luckily I just needed to filter Nones out so I could change it to
if value is not NoneThis also removes
Nonevalues when the field hasallow_none=True. To avoid this, we’d need to iterate over the Schema fields. But then it’s not that easy because the name of the fields may not match (due to attribute/dump_to/prefix…).I didn’t try it but I think it could be done clean and easy in
Marshaller.serialize()by adding askip_noneparameter:https://github.com/marshmallow-code/marshmallow/blob/dev/marshmallow/marshalling.py#
OK, reading https://github.com/marshmallow-code/marshmallow/commit/a3908d36967ac30764ceb66c52c99357425fbdec, I realize this looks like the
skip_missingyou removed…When serializing, marshmallow ignores missing attributes (attributes that raise an
AttributeError). I’ve been happy with this when using @touilleMan’s umongo (MongoDB ODM) because when a value is missing,object.attributeraisesAttributeError. Now, I’m trying to work with SQLAlchemy and missing fields are expressed asNoneso my API spits a lot ofnullvalues. I’m a total SQLAlchemy beginner so I may be missing something. But I had the same issue when dealing with simple objects. Looks like umongo’s handy attribute management is the exception rather than the rule and the solution should be on Marshmallow’s side.What I finally did to fix this is to implement a custom object that will not be considered as None and that I can handle however I want:
I solve this trouble in my library (marshmallow intregration with mongoengine) by creating a mixin class which overload the
_serializemethodsee: https://github.com/touilleMan/marshmallow-mongoengine/blob/master/marshmallow_mongoengine/fields.py#L7
@Bachmann1234
True; in these cases, you can just make
SKIP_VALUESa list.@makmanalp
Would you mind posting code that produces this error?
Are you planning on updating the docs? Making code changes?
Thank you!
What if i need all my fields with None value have been ignored in schema? I need to create some Meta class? Here is my case: In frontend i have a backbone model and it fetch data from backend, backend returns this None values and when i save model, i get errors - “Field may not be null”, because i sent the data that i got from backend, i think it’s a weird behaviour
I think we need to have skip_if argument:
@sloria If it’s ok i can create PR