Skip to content

Commit 09c7797

Browse files
authored
Clean up how _changed_fields are set in BaseDocument._from_son (#2090)
1 parent 22f3c70 commit 09c7797

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

mongoengine/base/document.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525

2626

2727
class BaseDocument(object):
28+
# TODO simplify how `_changed_fields` is used.
29+
# Currently, handling of `_changed_fields` seems unnecessarily convoluted:
30+
# 1. `BaseDocument` defines `_changed_fields` in its `__slots__`, yet it's
31+
# not setting it to `[]` (or any other value) in `__init__`.
32+
# 2. `EmbeddedDocument` sets `_changed_fields` to `[]` it its overloaded
33+
# `__init__`.
34+
# 3. `Document` does NOT set `_changed_fields` upon initialization. The
35+
# field is primarily set via `_from_son` or `_clear_changed_fields`,
36+
# though there are also other methods that manipulate it.
37+
# 4. The codebase is littered with `hasattr` calls for `_changed_fields`.
2838
__slots__ = ('_changed_fields', '_initialised', '_created', '_data',
2939
'_dynamic_fields', '_auto_id_field', '_db_field_map',
3040
'__weakref__')
@@ -665,9 +675,7 @@ def _get_collection_name(cls):
665675

666676
@classmethod
667677
def _from_son(cls, son, _auto_dereference=True, only_fields=None, created=False):
668-
"""Create an instance of a Document (subclass) from a PyMongo
669-
SON.
670-
"""
678+
"""Create an instance of a Document (subclass) from a PyMongo SON."""
671679
if not only_fields:
672680
only_fields = []
673681

@@ -690,7 +698,6 @@ def _from_son(cls, son, _auto_dereference=True, only_fields=None, created=False)
690698
if class_name != cls._class_name:
691699
cls = get_document(class_name)
692700

693-
changed_fields = []
694701
errors_dict = {}
695702

696703
fields = cls._fields
@@ -720,8 +727,13 @@ def _from_son(cls, son, _auto_dereference=True, only_fields=None, created=False)
720727
if cls.STRICT:
721728
data = {k: v for k, v in iteritems(data) if k in cls._fields}
722729

723-
obj = cls(__auto_convert=False, _created=created, __only_fields=only_fields, **data)
724-
obj._changed_fields = changed_fields
730+
obj = cls(
731+
__auto_convert=False,
732+
_created=created,
733+
__only_fields=only_fields,
734+
**data
735+
)
736+
obj._changed_fields = []
725737
if not _auto_dereference:
726738
obj._fields = fields
727739

0 commit comments

Comments
 (0)