Skip to content

Commit b42b760

Browse files
committed
Merge branch 'fix-reloading-strict' of https://github.com/paularmand/mongoengine into fix-reloading-strict and bumped version.
# Conflicts: # AUTHORS
2 parents bf6f4c4 + 6133f04 commit b42b760

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,5 @@ that much better:
231231
* Lars Butler (https://github.com/larsbutler)
232232
* George Macon (https://github.com/gmacon)
233233
* Ashley Whetter (https://github.com/AWhetter)
234+
* Paul-Armand Verhaegen (https://github.com/paularmand)
234235
* Steven Rossiter (https://github.com/BeardedSteve)

docs/changelog.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
Changelog
33
=========
44

5-
Changes in 0.10.4 - DEV
6-
=======================
5+
Changes in 0.10.5
6+
=================
7+
- Fix for reloading of strict with special fields. #1156
8+
9+
Changes in 0.10.4
10+
=================
711
- SaveConditionError is now importable from the top level package. #1165
812
- upsert_one method added. #1157
913

@@ -18,7 +22,7 @@ Changes in 0.10.2
1822
- ReferenceFields now support abstract document types. #837
1923

2024
Changes in 0.10.1
21-
=======================
25+
=================
2226
- Fix infinite recursion with CASCADE delete rules under specific conditions. #1046
2327
- Fix CachedReferenceField bug when loading cached docs as DBRef but failing to save them. #1047
2428
- Fix ignored chained options #842

mongoengine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
__all__ = (list(document.__all__) + fields.__all__ + connection.__all__ +
1515
list(queryset.__all__) + signals.__all__ + list(errors.__all__))
1616

17-
VERSION = (0, 10, 4)
17+
VERSION = (0, 10, 5)
1818

1919

2020
def get_version():

mongoengine/document.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,16 @@ def reload(self, *fields, **kwargs):
604604
if not fields or field in fields:
605605
try:
606606
setattr(self, field, self._reload(field, obj[field]))
607-
except KeyError:
608-
# If field is removed from the database while the object
609-
# is in memory, a reload would cause a KeyError
610-
# i.e. obj.update(unset__field=1) followed by obj.reload()
611-
delattr(self, field)
607+
except (KeyError, AttributeError):
608+
try:
609+
# If field is a special field, e.g. items is stored as _reserved_items,
610+
# an KeyError is thrown. So try to retrieve the field from _data
611+
setattr(self, field, self._reload(field, obj._data.get(field)))
612+
except KeyError:
613+
# If field is removed from the database while the object
614+
# is in memory, a reload would cause a KeyError
615+
# i.e. obj.update(unset__field=1) followed by obj.reload()
616+
delattr(self, field)
612617

613618
self._changed_fields = obj._changed_fields
614619
self._created = False

tests/document/instance.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,28 @@ class Foo(Document):
571571
except Exception:
572572
self.assertFalse("Threw wrong exception")
573573

574+
def test_reload_of_non_strict_with_special_field_name(self):
575+
"""Ensures reloading works for documents with meta strict == False
576+
"""
577+
class Post(Document):
578+
meta = {
579+
'strict': False
580+
}
581+
title = StringField()
582+
items = ListField()
583+
584+
Post.drop_collection()
585+
586+
Post._get_collection().insert_one({
587+
"title": "Items eclipse",
588+
"items": ["more lorem", "even more ipsum"]
589+
})
590+
591+
post = Post.objects.first()
592+
post.reload()
593+
self.assertEqual(post.title, "Items eclipse")
594+
self.assertEqual(post.items, ["more lorem", "even more ipsum"])
595+
574596
def test_dictionary_access(self):
575597
"""Ensure that dictionary-style field access works properly.
576598
"""

0 commit comments

Comments
 (0)