Skip to content

Fix dynamic embedded document updates #2882

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

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
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Development
- BugFix - Calling .clear on a ListField wasn't being marked as changed (and flushed to db upon .save()) #2858
- Improve error message in case a document assigned to a ReferenceField wasn't saved yet #1955
- BugFix - Take `where()` into account when using `.modify()`, as in MyDocument.objects().where("this[field] >= this[otherfield]").modify(field='new') #2044
- BugFix - Unable to add new fields during `QuerySet.update` on `DynamicEmbeddedDocument` fields #2486

Changes in 0.29.0
=================
Expand Down
12 changes: 12 additions & 0 deletions mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,12 @@ def lookup_member(self, member_name):
if field:
return field

# DynamicEmbeddedDocuments should always return a field except for positional operators
if any(
doc_type._dynamic for doc_type in doc_and_subclasses
) and member_name not in ("$", "S"):
return DynamicField(db_field=member_name)

def prepare_query_value(self, op, value):
if value is not None and not isinstance(value, self.document_type):
# Short circuit for special operators, returning them as is
Expand Down Expand Up @@ -837,6 +843,12 @@ def lookup_member(self, member_name):
if field:
return field

# DynamicEmbeddedDocuments should always return a field except for positional operators
if any(
document_choice._dynamic for document_choice in document_choices
) and member_name not in ("$", "S"):
return DynamicField(db_field=member_name)

def to_mongo(self, document, use_db_field=True, fields=None):
if document is None:
return None
Expand Down
43 changes: 43 additions & 0 deletions tests/fields/test_embedded_document_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from mongoengine import (
Document,
DynamicEmbeddedDocument,
EmbeddedDocument,
EmbeddedDocumentField,
EmbeddedDocumentListField,
Expand Down Expand Up @@ -224,6 +225,34 @@ class Record(Document):

assert Record.objects(posts__title="foo").count() == 2

def test_update_dynamic_embedded_document_with_new_fields(self):
class Wheel(DynamicEmbeddedDocument):
position = StringField()

class Car(Document):
wheels = EmbeddedDocumentListField(Wheel)

car = Car(
wheels=[
Wheel(position="front-passenger"),
Wheel(position="rear-passenger"),
Wheel(position="front-driver"),
Wheel(position="rear-driver"),
]
).save()

Car.objects(wheels__position="front-driver").update(
set__wheels__S__damaged=True
)
car.reload()

for wheel in car.wheels:
if wheel.position == "front-driver":
assert wheel.damaged
else:
with pytest.raises(AttributeError):
wheel.damaged


class TestGenericEmbeddedDocumentField(MongoDBTestCase):
def test_generic_embedded_document(self):
Expand Down Expand Up @@ -455,3 +484,17 @@ class Person(Document):

copied_map_emb_doc = deepcopy(doc.wallet_map)
assert copied_map_emb_doc["test"]._instance is None

def test_update_dynamic_embedded_document_with_new_fields(self):
class Laptop(DynamicEmbeddedDocument):
operating_system = StringField()

class Backpack(Document):
content = GenericEmbeddedDocumentField(choices=[Laptop])

backpack = Backpack(content=Laptop(operating_system="Windows")).save()

Backpack.objects.update(set__content__manufacturer="Acer")
backpack.reload()

assert backpack.content.manufacturer == "Acer"