我正在创建两个级别的嵌套嵌入文档(嵌入文档中的嵌入文档)
代码如下:
from mongoengine import *
class CommentDetails(EmbeddedDocument):
name = StringField()
category = StringField()
class Comment(EmbeddedDocument):
content = StringField()
comments = ListField(EmbeddedDocumentField(CommentDetails))
class Page(Document):
comments = ListField(EmbeddedDocumentField(Comment))
comment1 = Comment(content='Good work!',comments=CommentDetails(name='John',category='fashion'))
comment2 = Comment(content='Nice article!',comments=CommentDetails(name='Mike',category='tech'))
page = Page(comments=[comment1, comment2])
page.save()
运行时会出现以下错误:
ValidationError: ValidationError (Page:None) (comments.Only lists and tuples may be used in a list field >1.comments.Only lists and tuples may be used in a list field: ['comments'])
我尝试使用单个嵌套文档,它可以工作,如果我不使用EmbeddedDocuments作为列表,它也可以工作。但不确定为什么它不适用于多层次的嵌入式文档列表
问题来自以下两行:
comments
应该是一个列表,但您提供了一个对象使用以下命令:
相关问题 更多 >
编程相关推荐