Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix error if DBRef exists in DynamicEmbeddedDocument #2564

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mongoengine/base/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ def __expand_dynamic_values(self, name, value):
# If the value is a dict with '_cls' in it, turn it into a document
is_dict = isinstance(value, dict)
if is_dict and "_cls" in value:

# If the value is a referenced document, dereference it
if "_ref" in value:
_dereference = _import_class("DeReference")()
documents = _dereference([value])
if documents:
return documents[0]

cls = get_document(value["_cls"])
return cls(**value)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_dereference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,28 @@ class Page(Document):
page = Page.objects.first()
assert page.tags[0] == page.posts[0].tags[0]

def test_dereferencing_dynamic_embedded_field_referencefield(self):
class Tag(Document):
meta = {"collection": "tags"}
name = StringField()

class Post(DynamicEmbeddedDocument):
pass

class Page(Document):
meta = {"collection": "pages"}
post = EmbeddedDocumentField(Post)

Tag.drop_collection()
Page.drop_collection()

tag = Tag(name="test").save()
post = Post(book_tag=tag)
Page(post=post).save()

page = Page.objects.first()
assert page.post.book_tag == post.book_tag

def test_select_related_follows_embedded_referencefields(self):
class Song(Document):
title = StringField()
Expand Down