MongoEngine / MongoEngine/mongoengine

Document inherited from DynamicDocument throws ValidationError on EmbeddedDocument field

Open
#1,803 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

Document inherited from DynamicDocument throws Validation Error: Invalid embedded document instance provided to an EmbeddedDocumentField.

class MyEmbDoc(EmbeddedDocument):
    emb_field = StringField()

class MyDoc(DynamicDocument):
   emb_doc = EmbeddedDocumentField(MyEmbDoc, )

creating an instance of MyDoc from data in the form:

data_dict = { "emb_doc": {"emb_field": "foo"} }
using MyDoc(**data_dict) will throw a "ValidationError (MyDoc:None) (Invalid embedded document instance provided to an EmbeddedDocumentField: ['emb_doc'])",

However, the same MyDoc class if inherited from Document class does not have this issue. Debugging shows that the issue lies in BaseDocument init method:

if self._dynamic: dynamic_data = {} for key, value in values.items(): if key in self._fields or key == '_id': setattr(self, key, value) elif self._dynamic: dynamic_data[key] = value else: FileField = _import_class('FileField') for key, value in values.items(): if key == '__auto_convert': continue 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) if field and not isinstance(field, FileField): **value = field.to_python(value)** setattr(self, key, value) else: self._data[key] = value
field.to_python(value) calls the _from_son method that casts the nested dictionary into an object of class MyEmbDoc for instances of Document subclasses but not for those of DynamicDocument.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in BaseDocument.init and trace the dynamic and non-dynamic branches, then follow EmbeddedDocumentField.to_python and _from_son for the nested value. Reproduce the MyDoc(**data_dict) example and verify that a DynamicDocument accepts the dictionary as a MyEmbDoc without raising ValidationError, while the existing Document behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.