Python mongoengine 模块,EmbeddedDocumentField() 实例源码

我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用mongoengine.EmbeddedDocumentField()

项目:mongoengine_utils    作者:aiscenblue    | 项目源码 | 文件源码
def __set_gj_flag_sub_field(self, instance, fld, cur_depth):
        """Set $$good_json$$ flag to subfield."""
        from .fields import FollowReferenceField

        def set_good_json(fld):
            setattr(fld, "$$good_json$$", True)
            setattr(fld, "$$cur_depth$$", cur_depth)

        @singledispatch
        def set_flag_recursive(fld, instance):
            set_good_json(fld)

        @set_flag_recursive.register(db.ListField)
        def set_flag_list(fld, instance):
            set_good_json(fld.field)

        @set_flag_recursive.register(db.EmbeddedDocumentField)
        def set_flag_emb(fld, instance):
            if isinstance(instance, Helper):
                instance.begin_goodjson(cur_depth)

        @set_flag_recursive.register(FollowReferenceField)
        def set_flag_self(fld, instance):
            set_good_json(fld)

        set_flag_recursive(fld, instance)
项目:mongoengine_utils    作者:aiscenblue    | 项目源码 | 文件源码
def __unset_gj_flag_sub_field(self, instance, fld, cur_depth):
        """Unset $$good_json$$ to subfield."""
        from .fields import FollowReferenceField

        def unset_flag(fld):
            setattr(fld, "$$good_json$$", None)
            setattr(fld, "$$cur_depth$$", None)
            delattr(fld, "$$good_json$$")
            delattr(fld, "$$cur_depth$$")

        @singledispatch
        def unset_flag_recursive(fld, instance):
            unset_flag(fld)

        @unset_flag_recursive.register(db.ListField)
        def unset_flag_list(fld, instance):
            unset_flag(fld.field)

        @unset_flag_recursive.register(db.EmbeddedDocumentField)
        def unset_flag_emb(fld, instance):
            if isinstance(instance, Helper):
                instance.end_goodjson(cur_depth)

        @unset_flag_recursive.register(FollowReferenceField)
        def unset_flag_self(fld, instance):
            unset_flag(fld)

        unset_flag_recursive(fld, instance)
项目:django-mongo-rest    作者:TrueSkills    | 项目源码 | 文件源码
def _document_typeof(doc_cls, field_name):
    try:
        orm_field = doc_cls._fields[field_name]
    except (KeyError, AttributeError):
        return None

    if isinstance(orm_field, ListField):
        orm_field = orm_field.field
    if isinstance(orm_field, (ReferenceField, EmbeddedDocumentField)):
        return orm_field.document_type

    return None
项目:mongoengine_utils    作者:aiscenblue    | 项目源码 | 文件源码
def _follow_reference(self, max_depth, current_depth,
                          use_db_field, *args, **kwargs):
        from .fields import FollowReferenceField
        ret = {}
        for fldname in self:
            fld = self._fields.get(fldname)
            is_list = isinstance(fld, db.ListField)
            target = fld.field if is_list else fld

            if all([
                isinstance(
                    target, (db.ReferenceField, db.EmbeddedDocumentField)
                ), not isinstance(target, FollowReferenceField)
            ]):
                value = None
                if is_list:
                    value = []
                    for doc in getattr(self, fldname, []):
                        value.append(json.loads((
                            target.document_type.objects(
                                id=doc.id
                            ).get() if isinstance(doc, DBRef) else doc
                        ).to_json(
                            follow_reference=True,
                            max_depth=max_depth,
                            current_depth=current_depth + 1,
                            use_db_field=use_db_field,
                            *args, **kwargs
                        )))
                else:
                    doc = getattr(self, fldname, None)
                    value = json.loads(
                        (
                            target.document_type.objects(
                                id=doc.id
                            ).get() if isinstance(doc, DBRef) else doc
                        ).to_json(
                            follow_reference=True,
                            max_depth=max_depth,
                            current_depth=current_depth + 1,
                            use_db_field=use_db_field,
                            *args, **kwargs
                        )
                    ) if doc else doc
                if value is not None:
                    ret.update({fldname: value})
        return ret