question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty DictField in ListField

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
touilleMancommented, Apr 25, 2017

Seems pretty reasonable to me, @lafrech feel free to make this change into the master 😉

1reaction
lafrechcommented, Apr 25, 2017

I modified _serialize_to_mongo like this:

    def _serialize_to_mongo(self, obj):
        if obj is None:
            return missing
        return dict(obj)

It only breaks the test that checks precisely the behavior I’m changing:

        # Empty dict is considered as missing field
        d2.set('dict', {})
>       assert d2.to_mongo() == {}
E       AssertionError: assert {'in_mongo_dict': {}} == {}
E         Left contains more items:
E         {'in_mongo_dict': {}}
E         Use -v to get the full diff

Serializing empty dict as {} rather than missing seems reasonable to me, not only in the “dict in the list” case.

@touilleMan, any opinion about this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found