Skip to content

Fix error if DBRef exists in DynamicEmbeddedDocument#2564

Open
spengjie wants to merge 1 commit into
MongoEngine:masterfrom
spengjie:master
Open

Fix error if DBRef exists in DynamicEmbeddedDocument#2564
spengjie wants to merge 1 commit into
MongoEngine:masterfrom
spengjie:master

Conversation

@spengjie

Copy link
Copy Markdown

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.

mongoengine.errors.FieldDoesNotExist: The fields "{'_ref'}" do not exist on the document "XXX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant