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
评论列表
文章目录