Skip to content

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant