Skip to content

Fix error if DBRef exists in DynamicEmbeddedDocument #2564

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

spengjie
Copy link

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"
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant