Skip to content

Commit

Permalink
Merge pull request #2485 from janste63/fix_2484
Browse files Browse the repository at this point in the history
Test case and proposed solution for #2484
  • Loading branch information
bagerard committed Mar 29, 2021
2 parents 1669f0c + da173cf commit f0fad6d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,4 @@ that much better:
* Agustin Barto (https://github.com/abarto)
* Stankiewicz Mateusz (https://github.com/mas15)
* Felix Schultheiß (https://github.com/felix-smashdocs)
* Jan Stein (https://github.com/janste63)
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
Development
===========
- (Fill this out as you fix issues and develop your features).
- Bug fix: ignore LazyReferenceFields when clearing _changed_fields #2484
- Improve connection doc #2481

Changes in 0.23.0
Expand Down
10 changes: 9 additions & 1 deletion mongoengine/base/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,9 @@ def _nestable_types_changed_fields(changed_fields, base_key, data):
def _get_changed_fields(self):
"""Return a list of all fields that have explicitly been changed."""
EmbeddedDocument = _import_class("EmbeddedDocument")
LazyReferenceField = _import_class("LazyReferenceField")
ReferenceField = _import_class("ReferenceField")
GenericLazyReferenceField = _import_class("GenericLazyReferenceField")
GenericReferenceField = _import_class("GenericReferenceField")
SortedListField = _import_class("SortedListField")

Expand All @@ -641,7 +643,13 @@ def _get_changed_fields(self):
changed_fields += [f"{key}{k}" for k in changed if k]
elif isinstance(data, (list, tuple, dict)):
if hasattr(field, "field") and isinstance(
field.field, (ReferenceField, GenericReferenceField)
field.field,
(
LazyReferenceField,
ReferenceField,
GenericLazyReferenceField,
GenericReferenceField,
),
):
continue
elif isinstance(field, SortedListField) and field._ordering:
Expand Down
20 changes: 20 additions & 0 deletions tests/fields/test_lazy_reference_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,26 @@ class Book(Document):
assert isinstance(ref.author, LazyReference)
assert isinstance(ref.author.id, ObjectId)

def test_lazy_reference_in_list_with_changed_element(self):
class Animal(Document):
name = StringField()
tag = StringField()

class Ocurrence(Document):
in_list = ListField(LazyReferenceField(Animal))

Animal.drop_collection()
Ocurrence.drop_collection()

animal1 = Animal(name="doggo").save()

animal1.tag = "blue"

occ = Ocurrence(in_list=[animal1]).save()
animal1.save()
assert isinstance(occ.in_list[0], LazyReference)
assert occ.in_list[0].pk == animal1.pk


class TestGenericLazyReferenceField(MongoDBTestCase):
def test_generic_lazy_reference_simple(self):
Expand Down

0 comments on commit f0fad6d

Please sign in to comment.