Skip to content

Commit

Permalink
Merge branch 'fix-reloading-strict' of https://github.com/paularmand/…
Browse files Browse the repository at this point in the history
…mongoengine into fix-reloading-strict and bumped version.

# Conflicts:
#	AUTHORS
  • Loading branch information
omerzimp committed Nov 30, 2015
2 parents bf6f4c4 + 6133f04 commit b42b760
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -231,4 +231,5 @@ that much better:
* Lars Butler (https://github.com/larsbutler)
* George Macon (https://github.com/gmacon)
* Ashley Whetter (https://github.com/AWhetter)
* Paul-Armand Verhaegen (https://github.com/paularmand)
* Steven Rossiter (https://github.com/BeardedSteve)
10 changes: 7 additions & 3 deletions docs/changelog.rst
Expand Up @@ -2,8 +2,12 @@
Changelog
=========

Changes in 0.10.4 - DEV
=======================
Changes in 0.10.5
=================
- Fix for reloading of strict with special fields. #1156

Changes in 0.10.4
=================
- SaveConditionError is now importable from the top level package. #1165
- upsert_one method added. #1157

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

Changes in 0.10.1
=======================
=================
- Fix infinite recursion with CASCADE delete rules under specific conditions. #1046
- Fix CachedReferenceField bug when loading cached docs as DBRef but failing to save them. #1047
- Fix ignored chained options #842
Expand Down
2 changes: 1 addition & 1 deletion mongoengine/__init__.py
Expand Up @@ -14,7 +14,7 @@
__all__ = (list(document.__all__) + fields.__all__ + connection.__all__ +
list(queryset.__all__) + signals.__all__ + list(errors.__all__))

VERSION = (0, 10, 4)
VERSION = (0, 10, 5)


def get_version():
Expand Down
15 changes: 10 additions & 5 deletions mongoengine/document.py
Expand Up @@ -604,11 +604,16 @@ def reload(self, *fields, **kwargs):
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)
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
Expand Down
22 changes: 22 additions & 0 deletions tests/document/instance.py
Expand Up @@ -571,6 +571,28 @@ class Foo(Document):
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.
"""
Expand Down

0 comments on commit b42b760

Please sign in to comment.