Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,14 @@ def prepare_query_value(self, op, value):

return super().prepare_query_value(op, value)

def to_python(self, value):
to_python = getattr(self.field, "to_python", None)
return (
{k: to_python(v) for k, v in value.items()}
if to_python and value
else value or None
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would convert a {} to None

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class MyModel(db.Document):
    data_dict_1 = db.DictField(required=False)

m = MyModel()
m.data_dict_1 = {}
m.save()

model = MyModel.objects.first()
print(model.data_dict_1)

Outputs {}.

)


class MapField(DictField):
"""A field that maps a name to a specified field type. Similar to
Expand Down
Loading