From fb4e9c37728d330dc01baadc9fb2ab5c96d4566d Mon Sep 17 00:00:00 2001 From: Paul-Armand Verhaegen Date: Tue, 10 Nov 2015 20:43:49 +0100 Subject: [PATCH 1/5] fix for reloading of strict with special fields --- mongoengine/document.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index 9d2d9c5f..0cdfeeb6 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -594,12 +594,17 @@ class Document(BaseDocument): for field in obj._data: if not fields or field in fields: try: - setattr(self, field, self._reload(field, obj[field])) - except KeyError: - # If field is removed from the database while the object - # is in memory, a reload would cause a KeyError - # i.e. obj.update(unset__field=1) followed by obj.reload() - delattr(self, field) + setattr(self, field, self._reload(field, obj[field])) + except (KeyError, AttributeError): + try: + # If field is a special field, e.g. items is stored as _reserved_items, + # an KeyError is thrown. So try to retrieve the field from _data + setattr(self, field, self._reload(field, obj._data.get(field))) + except KeyError: + # If field is removed from the database while the object + # is in memory, a reload would cause a KeyError + # i.e. obj.update(unset__field=1) followed by obj.reload() + delattr(self, field) self._changed_fields = obj._changed_fields self._created = False From 6e745e9882edd0cf459166b289515cf4ee3145ef Mon Sep 17 00:00:00 2001 From: Paul-Armand Verhaegen Date: Tue, 10 Nov 2015 21:13:24 +0100 Subject: [PATCH 2/5] fixed wrong indentation style --- mongoengine/document.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index 0cdfeeb6..bdae0a38 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -594,13 +594,13 @@ class Document(BaseDocument): for field in obj._data: if not fields or field in fields: try: - setattr(self, field, self._reload(field, obj[field])) + setattr(self, field, self._reload(field, obj[field])) except (KeyError, AttributeError): - try: - # If field is a special field, e.g. items is stored as _reserved_items, - # an KeyError is thrown. So try to retrieve the field from _data - setattr(self, field, self._reload(field, obj._data.get(field))) - except KeyError: + try: + # If field is a special field, e.g. items is stored as _reserved_items, + # an KeyError is thrown. So try to retrieve the field from _data + setattr(self, field, self._reload(field, obj._data.get(field))) + except KeyError: # If field is removed from the database while the object # is in memory, a reload would cause a KeyError # i.e. obj.update(unset__field=1) followed by obj.reload() From 3c8906494fb57e2b1c47b40091dea1b9eef24f4f Mon Sep 17 00:00:00 2001 From: Paul-Armand Verhaegen Date: Sun, 15 Nov 2015 15:31:22 +0100 Subject: [PATCH 3/5] Added #1156 to changelog --- docs/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 51f0967f..c9766c84 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -13,6 +13,7 @@ Changes in 0.10.1 - DEV - Remove test dependencies (nose and rednose) from install dependencies list. #1079 - Recursively build query when using elemMatch operator. #1130 - Fix instance back references for lists of embedded documents. #1131 +- Fix for reloading of strict with special fields. #1156 Changes in 0.10.0 ================= From ed8174fe367c2c714ccc5addf7d7b02bff63592d Mon Sep 17 00:00:00 2001 From: Paul-Armand Verhaegen Date: Sun, 15 Nov 2015 15:32:26 +0100 Subject: [PATCH 4/5] Added Paul-Armand Verhaegen to contributor list --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 4d5e69a3..df6ef611 100644 --- a/AUTHORS +++ b/AUTHORS @@ -229,3 +229,4 @@ that much better: * Emile Caron (https://github.com/emilecaron) * Amit Lichtenberg (https://github.com/amitlicht) * Lars Butler (https://github.com/larsbutler) + * Paul-Armand Verhaegen (https://github.com/paularmand) From 3c18f79ea45996e715c4d9af394c7d23947c2ffe Mon Sep 17 00:00:00 2001 From: Paul-Armand Verhaegen Date: Fri, 27 Nov 2015 23:45:25 +0100 Subject: [PATCH 5/5] Added test for reloading of strict with special fields #1156 --- tests/document/instance.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/document/instance.py b/tests/document/instance.py index 56e6765a..fe8ed1bd 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -557,6 +557,28 @@ class InstanceTest(unittest.TestCase): except Exception: self.assertFalse("Threw wrong exception") + def test_reload_of_non_strict_with_special_field_name(self): + """Ensures reloading works for documents with meta strict == False + """ + class Post(Document): + meta = { + 'strict': False + } + title = StringField() + items = ListField() + + Post.drop_collection() + + Post._get_collection().insert_one({ + "title": "Items eclipse", + "items": ["more lorem", "even more ipsum"] + }) + + post = Post.objects.first() + post.reload() + self.assertEqual(post.title, "Items eclipse") + self.assertEqual(post.items, ["more lorem", "even more ipsum"]) + def test_dictionary_access(self): """Ensure that dictionary-style field access works properly. """