mongoengine
mongoengine copied to clipboard
Fix error if DBRef exists in DynamicEmbeddedDocument
If assign a document to DynamicEmbeddedDocument, DBRef will be used to store the reference. The data can be stored successfully. But mongoengine.errors.FieldDoesNotExist error raised while reading the data from the database.
class Tag(Document):
meta = {"collection": "tags"}
name = StringField()
class Post(DynamicEmbeddedDocument):
pass
class Page(Document):
meta = {"collection": "pages"}
post = EmbeddedDocumentField(Post)
tag = Tag(name="test").save()
post = Post(book_tag=tag)
Page(post=post).save()
page = Page.objects.first() # mongoengine.errors.FieldDoesNotExist raised here
Error message: mongoengine.errors.FieldDoesNotExist: The fields "{'_ref'}" do not exist on the document "XXX" Solution: Dereference the field value if it is a dict with both '_cls' and '_ref' in it.