mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Added _instance for EmbeddedDocument then accessing dict values

Open GodMorduk opened this issue 2 years ago • 0 comments

In cases when we have a DictField and we have an EmbeddedDocument as a value of the dict, the values doesn't get _instance, thus not allowing us to reference parent document.

But then we get access value directly, we can have _instance set correctly. So basically there is a workaround, but the code became a bit messy.

Consider the following example, where "vars" is DictField with string in key and EmbeddedDocument in value:

# that won't work, would print "None"
for var in self.vars.values():
    print(var._instance)

# that is working, would print correct object
for var in self.vars:
   print(self.vars[var]._instance)

This small PR fixes it, as it seems like a bug, not an intended behaviour. Haven't found any issues with that, there is simply one extra isinstance check and impact on perfomance should not be noticable. It also shoud not cause any incompability with any existing projects using MongoEngine.

GodMorduk avatar Apr 27 '23 00:04 GodMorduk