follow_reference.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:mongoengine_utils 作者: aiscenblue 项目源码 文件源码
def to_mongo(self, document, **kwargs):
        """
        Convert to python-typed dict.

        Parameters:
            document: The document.
        """
        cur_depth = getattr(self, "$$cur_depth$$", self.max_depth)
        if not getattr(self, "$$good_json$$", None) or \
                cur_depth >= self.max_depth:
            return super(FollowReferenceField, self).to_mongo(
                document, **kwargs
            )
        doc = document
        if isinstance(document, db.Document):
            if document.pk is None and self.id_check:
                self.error("The referenced document needs ID.")
        else:
            doc = self.document_type.objects(
                pk=super(
                    FollowReferenceField, self
                ).to_mongo(document, **kwargs)
            ).get()
        if isinstance(doc, Document):
            doc.begin_goodjson(cur_depth + 1)
        ret = doc.to_mongo(**kwargs)
        if isinstance(doc, Document):
            doc.end_goodjson(cur_depth + 1)
        if "_id" in ret and issubclass(self.document_type, Document):
            ret["id"] = ret.pop("_id", None)
        return ret
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号