在内部,这两个领域之间有什么区别?这些字段在mongo中映射到什么样的模式?此外,如何将具有关系的文档添加到这些字段中?例如,如果我使用
from mongoengine import *
class User(Document):
name = StringField()
class Comment(EmbeddedDocument):
text = StringField()
tag = StringField()
class Post(Document):
title = StringField()
author = ReferenceField(User)
comments = ListField(EmbeddedDocumentField(Comment))
Run Code Online (Sandbox Code Playgroud)
并打电话
>>> some_author = User.objects.get(name="ExampleUserName")
>>> post = Post.objects.get(author=some_author)
>>> post.comments
[]
>>> comment = Comment(text="cool post", tag="django")
>>> comment.save()
>>>
Run Code Online (Sandbox Code Playgroud)
我应该使用post.comments.append(评论)或post.comments + =评论来附加此文档吗?我最初的问题源于对如何处理这个问题的困惑.