From de0e5583a558238386043ebc25e3863a32190f51 Mon Sep 17 00:00:00 2001 From: David Bordeynik Date: Sun, 29 Mar 2015 09:28:26 +0300 Subject: [PATCH] Fix #595: Support += and *= for ListField --- docs/changelog.rst | 4 ++++ mongoengine/base/datastructures.py | 8 ++++++++ tests/fields/fields.py | 12 ++++++++++++ 3 files changed, 24 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 59398339..8f5904b8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,10 @@ Changelog Changes in 0.9.X - DEV ====================== +- Support += and *= for ListField #595 + +Changes in 0.9.0 +================ - Update FileField when creating a new file #714 - Added `EmbeddedDocumentListField` for Lists of Embedded Documents. #826 - ComplexDateTimeField should fall back to None when null=True #864 diff --git a/mongoengine/base/datastructures.py b/mongoengine/base/datastructures.py index bac67ddc..c9feb71a 100644 --- a/mongoengine/base/datastructures.py +++ b/mongoengine/base/datastructures.py @@ -156,6 +156,14 @@ class BaseList(list): self = state return self + def __iadd__(self, other): + self._mark_as_changed() + return super(BaseList, self).__iadd__(other) + + def __imul__(self, other): + self._mark_as_changed() + return super(BaseList, self).__imul__(other) + def append(self, *args, **kwargs): self._mark_as_changed() return super(BaseList, self).append(*args, **kwargs) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 64898995..5e7f34b5 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -946,6 +946,18 @@ class FieldTest(unittest.TestCase): BlogPost.objects.filter(info__0__test__exact='5').count(), 0) self.assertEqual( BlogPost.objects.filter(info__100__test__exact='test').count(), 0) + + post = BlogPost() + post.info = ['1', '2'] + post.save() + post = BlogPost.objects(info=['1', '2']).get() + post.info += ['3', '4'] + post.save() + self.assertEqual(BlogPost.objects(info=['1', '2', '3', '4']).count(), 1) + post = BlogPost.objects(info=['1', '2', '3', '4']).get() + post.info *= 2 + post.save() + self.assertEqual(BlogPost.objects(info=['1', '2', '3', '4', '1', '2', '3', '4']).count(), 1) BlogPost.drop_collection() def test_list_field_passed_in_value(self):