MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / EmbeddedDocumentField

Class EmbeddedDocumentField

mongoengine/fields.py:709–790  ·  view source on GitHub ↗

An embedded document field - with a declared document_type. Only valid values are subclasses of :class:`~mongoengine.EmbeddedDocument`.

Source from the content-addressed store, hash-verified

707
708
709class EmbeddedDocumentField(BaseField):
710 """An embedded document field - with a declared document_type.
711 Only valid values are subclasses of :class:`~mongoengine.EmbeddedDocument`.
712 """
713
714 def __init__(self, document_type, **kwargs):
715 if not (
716 isinstance(document_type, str)
717 or issubclass(document_type, EmbeddedDocument)
718 ):
719 self.error(
720 "Invalid embedded document class provided to an "
721 "EmbeddedDocumentField"
722 )
723
724 self.document_type_obj = document_type
725 super().__init__(**kwargs)
726
727 @property
728 def document_type(self):
729 if isinstance(self.document_type_obj, str):
730 if self.document_type_obj == RECURSIVE_REFERENCE_CONSTANT:
731 resolved_document_type = self.owner_document
732 else:
733 resolved_document_type = get_document(self.document_type_obj)
734
735 if not issubclass(resolved_document_type, EmbeddedDocument):
736 # Due to the late resolution of the document_type
737 # There is a chance that it won't be an EmbeddedDocument (#1661)
738 self.error(
739 "Invalid embedded document class provided to an "
740 "EmbeddedDocumentField"
741 )
742 self.document_type_obj = resolved_document_type
743
744 return self.document_type_obj
745
746 def to_python(self, value):
747 if not isinstance(value, self.document_type):
748 return self.document_type._from_son(
749 value, _auto_dereference=self._auto_dereference
750 )
751 return value
752
753 def to_mongo(self, value, use_db_field=True, fields=None):
754 if not isinstance(value, self.document_type):
755 return value
756 return self.document_type.to_mongo(value, use_db_field, fields)
757
758 def validate(self, value, clean=True):
759 """Make sure that the document instance is an instance of the
760 EmbeddedDocument subclass provided when the document was defined.
761 """
762 # Using isinstance also works for subclasses of self.document
763 if not isinstance(value, self.document_type):
764 self.error(
765 "Invalid embedded document instance provided to an "
766 "EmbeddedDocumentField"

Callers 15

DataDocClass · 0.90
BlogPostClass · 0.90
CategoryListClass · 0.90
TestClass · 0.90
BClass · 0.90
AClass · 0.90
MyDoc2Class · 0.90
PersonClass · 0.90
BasedocClass · 0.90
TreeNodeClass · 0.90
TreeClass · 0.90
CommentClass · 0.90

Calls

no outgoing calls