Empty DictField in ListField
See original GitHub issueWhen I give an empty dict (“{}”) to a DictField inside a ListField, I get this error when trying to save the object:
bson.errors.InvalidDocument: Cannot encode object: <marshmallow.missing>
A minimal example to reproduce:
... initialize db ...
from umongo import fields, Document
@instance.register
class ListTest(Document):
dict = fields.ListField(fields.DictField())
new = ListTest(dict=[{}])
new.commit()
The problem is, that the _serialize_to_mongo of DictField replaces empty dicts with the missing object:
def _serialize_to_mongo(self, obj):
if not obj:
return missing
return dict(obj)
See http://umongo.readthedocs.io/en/latest/_modules/umongo/fields.html#DictField
Why isn’t it possible to save an empty Dict? Especially in a ListField there is a difference between having an empty dict and not having an object at all.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Allow empty list in serializers.ListField - django - Stack Overflow
I am using Django REST framework 3.3 and am trying to serialize a list that could be empty with the provided serializers.ListField class ......
Read more >How to get ListField(child=serializer.DictField()) - Google Groups
AssertionError: `child` has not been instantiated. i got the data and want to make Owner and Manager data and connect M2M between Owner...
Read more >DictField in serializers - Django REST Framework
This article revolves around DictField in Serializers in Django REST ... allow_empty – Designates if empty dictionaries are allowed.
Read more >Python mongoengine.fields.ListField() Examples
DictField else: return self.build_unknown_field(field_name, ... Check that ListField is allowed to be empty, if required=False. Example #9 ...
Read more >mongoengine.fields — YETI 1.0 documentation
ListField ` designed specially to hold a list of embedded documents to ... note:: Required means it cannot be empty - as the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Seems pretty reasonable to me, @lafrech feel free to make this change into the master 😉
I modified
_serialize_to_mongo
like this:It only breaks the test that checks precisely the behavior I’m changing:
Serializing empty
dict
as{}
rather than missing seems reasonable to me, not only in the “dict
in thelist
” case.@touilleMan, any opinion about this?