Skip to content

Commit 31f6067

Browse files
committed
[fix] SortedListField: update whole list if order is changed
1 parent 42ba4a5 commit 31f6067

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

mongoengine/base/document.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ def _get_changed_fields(self, inspected=None):
547547
EmbeddedDocument = _import_class("EmbeddedDocument")
548548
DynamicEmbeddedDocument = _import_class("DynamicEmbeddedDocument")
549549
ReferenceField = _import_class("ReferenceField")
550+
SortedListField = _import_class("SortedListField")
550551
changed_fields = []
551552
changed_fields += getattr(self, '_changed_fields', [])
552553

@@ -577,6 +578,12 @@ def _get_changed_fields(self, inspected=None):
577578
if (hasattr(field, 'field') and
578579
isinstance(field.field, ReferenceField)):
579580
continue
581+
elif (isinstance(field, SortedListField) and field._ordering):
582+
# if ordering is affected whole list is changed
583+
if any(map(lambda d: field._ordering in d._changed_fields, data)):
584+
changed_fields.append(db_field_name)
585+
continue
586+
580587
self._nestable_types_changed_fields(
581588
changed_fields, key, data, inspected)
582589
return changed_fields

tests/fields/fields.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,13 @@ class BlogPost(Document):
916916
self.assertEqual(post.comments[0].content, comment2.content)
917917
self.assertEqual(post.comments[1].content, comment1.content)
918918

919+
post.comments[0].order = 2
920+
post.save()
921+
post.reload()
922+
923+
self.assertEqual(post.comments[0].content, comment1.content)
924+
self.assertEqual(post.comments[1].content, comment2.content)
925+
919926
BlogPost.drop_collection()
920927

921928
def test_reverse_list_sorting(self):

0 commit comments

Comments
 (0)