Skip to content

Commit fb4e9c3

Browse files
committed
fix for reloading of strict with special fields
1 parent 2c282f9 commit fb4e9c3

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

mongoengine/document.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,17 @@ def reload(self, *fields, **kwargs):
594594
for field in obj._data:
595595
if not fields or field in fields:
596596
try:
597-
setattr(self, field, self._reload(field, obj[field]))
598-
except KeyError:
599-
# If field is removed from the database while the object
600-
# is in memory, a reload would cause a KeyError
601-
# i.e. obj.update(unset__field=1) followed by obj.reload()
602-
delattr(self, field)
597+
setattr(self, field, self._reload(field, obj[field]))
598+
except (KeyError, AttributeError):
599+
try:
600+
# If field is a special field, e.g. items is stored as _reserved_items,
601+
# an KeyError is thrown. So try to retrieve the field from _data
602+
setattr(self, field, self._reload(field, obj._data.get(field)))
603+
except KeyError:
604+
# If field is removed from the database while the object
605+
# is in memory, a reload would cause a KeyError
606+
# i.e. obj.update(unset__field=1) followed by obj.reload()
607+
delattr(self, field)
603608

604609
self._changed_fields = obj._changed_fields
605610
self._created = False

0 commit comments

Comments
 (0)