Fix KeyError on reload() from a DynamicDocument

If the document is in memory and a field is deleted from the db,
calling reload() would raise a KeyError.
This commit is contained in:
André Ericson
2014-11-08 19:11:51 -03:00
parent 70942ac0f6
commit 3569529a84
3 changed files with 15 additions and 1 deletions

View File

@@ -543,7 +543,13 @@ class Document(BaseDocument):
for field in self._fields_ordered:
if not fields or field in fields:
setattr(self, field, self._reload(field, obj[field]))
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)
self._changed_fields = obj._changed_fields
self._created = False