use*_*508 11 schema json dictionary bson mongoengine
在内部,这两个领域之间有什么区别?这些字段在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 + =评论来附加此文档吗?我最初的问题源于对如何处理这个问题的困惑.
tbi*_*icr 10
EmbeddedDocumentField就像父文档的路径一样DictField,存储在mongo中的父文档的一条记录中.
保存EmbeddedDocument只保存父文档.
>>> some_author = User.objects.get(name="ExampleUserName")
>>> post = Post.objects.get(author=some_author)
>>> post.comments
[]
>>> comment = Comment(text="cool post", tag="django")
>>> post.comment.append(comment)
>>> post.save()
>>> post.comment
[<Comment object __unicode__>]
>>> Post.objects.get(author=some_author).comment
[<Comment object __unicode__>]
Run Code Online (Sandbox Code Playgroud)
请参阅文档:https://mongoengine-odm.readthedocs.org/en/latest/guide/defining-documents.html#embedded-documents.
| 归档时间: |
|
| 查看次数: |
4596 次 |
| 最近记录: |