Skip to content

error while using "db_field" parameter in multi-level embedded documents #904

@ms5

Description

@ms5

The following code is an example to reconstruct what's going wrong. The main problem seems to be the use of db_field in the second level of the embedded document (In the example this is class B)

data = {'b': {'c': [{'x': 1.0, 'y': 2.0}]}}

class C(EmbeddedDocument):
    x = FloatField(db_field='fx')
    y = FloatField(db_field='fy')

class B(EmbeddedDocument):
    c = ListField(EmbeddedDocumentField(C), db_field='fc')

class A(EmbeddedDocument):
    b = EmbeddedDocumentField(B, db_field='fb')

a = A(**data)
a.validate()

If I run this, it throws an exception:

  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/mongoengine/base/document.py", line 424, in validate
    raise ValidationError(message, errors=errors)
mongoengine.errors.ValidationError: ValidationError (A:None) (c.Invalid embedded document instance provided to an EmbeddedDocumentField: ['b'])

But it runs through if I remove the db_field attribute in class B:

class B(EmbeddedDocument):
    c = ListField(EmbeddedDocumentField(C))

While digging in to this problem I found out that the exception is actually correct when saying the Document has the wrong format.

while running a simple print on the working an non working version, the type differs.

print(a.b.c[0])
print(type(a.b.c[0]))

# wrong
{'x': 1.0, 'y': 2.0}
<class 'mongoengine.base.datastructures.BaseDict'>

# good
C object
<class '__main__.C'>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions