You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an issue with the updating of a DictField of a DynamicDocument that sometimes raises an exception.
It seems that there is a bug in mongoengine (more precisely in the _changed_fields) when we try to update a key of an inner dict, within the DictField that is at the top level of the Document.
As far as I dug, it seems to be because the inner dict is considered as a BaseDict, so when it is updated, the inner dict is added to _changed_fields but this inner dict isn't a field of the document, so there is an AttributeError on save() (see traceback below).
One more thing: this seem to be the case only with DynamicDocument and not with Document. And there is also no exception raised when the document came from the database.
Tested on versions:
Python 3.10.13
MongoEngine 0.27.0
PyMongo 4.6.1
Code to reproduce the bug:
frommongoengineimportconnect, DictField, DynamicDocumentconnect("test", host="mongodb://localhost:27018/test")
classExampleModel(DynamicDocument): # but there is no exception at all with `Document`root_dict=DictField(default=None)
ExampleModel.objects().delete() # just in caseexample=ExampleModel()
example.root_dict= {"inner_dict":{"key": "value"}}
example.save() # ✅ifexample.root_dict["inner_dict"]["key"]:
print("'inner_dict' is present in 'example'")
# ------------------# example.reload() # <-- with this line uncommented, no exception is raisedexample.root_dict["inner_dict"]["key"] ="NEW_value"try:
example.save() # raise "AttributeError" ❌exceptAttributeErrorasexc:
print(exc)
else:
print("No exception for 'example'")
# ------------------example_from_db=ExampleModel.objects().first()
example_from_db.root_dict["inner_dict"]["key"] ="NEW_value"try:
example_from_db.save() # don't raise any exception ✅exceptAttributeErrorasexc:
print(exc)
else:
print("No exception for 'example_from_db'")
Hi,
Details:
I have an issue with the updating of a
DictField
of aDynamicDocument
that sometimes raises an exception.It seems that there is a bug in
mongoengine
(more precisely in the_changed_fields
) when we try to update a key of an inner dict, within the DictField that is at the top level of theDocument
.As far as I dug, it seems to be because the inner dict is considered as a
BaseDict
, so when it is updated, the inner dict is added to_changed_fields
but this inner dict isn't a field of the document, so there is anAttributeError
onsave()
(see traceback below).One more thing: this seem to be the case only with
DynamicDocument
and not withDocument
. And there is also no exception raised when the document came from the database.Tested on versions:
3.10.13
0.27.0
4.6.1
Code to reproduce the bug:
Output of this program:
Complete traceback of the error on
.save()
:The text was updated successfully, but these errors were encountered: