detect when EmbeddedDocumentListField receives an EmbeddedDocument instance instead of a list. It is a common mistake and the error wasn't meaningful (was fired from _from_son)

(relates to #1464)
This commit is contained in:
Bastien Gérard
2018-09-04 22:25:53 +02:00
parent 032ace40d1
commit 193656e71b
2 changed files with 45 additions and 2 deletions

View File

@@ -313,11 +313,16 @@ class ComplexBaseField(BaseField):
if hasattr(value, 'to_python'):
return value.to_python()
BaseDocument = _import_class('BaseDocument')
if isinstance(value, BaseDocument):
# Something is wrong, return the value as it is
return value
is_list = False
if not hasattr(value, 'items'):
try:
is_list = True
value = {k: v for k, v in enumerate(value)}
value = {idx: v for idx, v in enumerate(value)}
except TypeError: # Not iterable return the value
return value