Skip to content

Fix db_field conflict bug #2171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix db_field conflict bug
  • Loading branch information
HuangYan committed Oct 2, 2019
commit f240aabb776b87e59d2bde51983663ea296ce609
18 changes: 10 additions & 8 deletions mongoengine/base/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def __init__(self, *args, **values):
)

__auto_convert = values.pop("__auto_convert", True)

__only_fields = set(values.pop("__only_fields", values))

_created = values.pop("_created", True)

signals.pre_init.send(self.__class__, document=self, values=values)
Expand Down Expand Up @@ -130,7 +128,6 @@ def __init__(self, *args, **values):
else:
FileField = _import_class("FileField")
for key, value in iteritems(values):
key = self._reverse_db_field_map.get(key, key)
if key in self._fields or key in ("id", "pk", "_cls"):
if __auto_convert and value is not None:
field = self._fields.get(key)
Expand Down Expand Up @@ -743,7 +740,6 @@ def _from_son(cls, son, _auto_dereference=True, only_fields=None, created=False)
data = {}
for key, value in iteritems(son):
key = str(key)
key = cls._db_field_map.get(key, key)
data[key] = value

# Return correct subclass for document type
Expand All @@ -756,19 +752,25 @@ def _from_son(cls, son, _auto_dereference=True, only_fields=None, created=False)
if not _auto_dereference:
fields = copy.deepcopy(fields)

_conflict_cached_data = {}
for field_name, field in iteritems(fields):
field._auto_dereference = _auto_dereference
if field.db_field in data:
value = data[field.db_field]
try:
data[field_name] = (
value if value is None else field.to_python(value)
)
if field_name in data:
_conflict_cached_data[field_name] = (
value if value is None else field.to_python(value)
)
else:
data[field_name] = (
value if value is None else field.to_python(value)
)
if field_name != field.db_field:
del data[field.db_field]
except (AttributeError, ValueError) as e:
errors_dict[field_name] = e

data.update(_conflict_cached_data)
if errors_dict:
errors = "\n".join(["%s - %s" % (k, v) for k, v in errors_dict.items()])
msg = "Invalid data to create a `%s` instance.\n%s" % (
Expand Down