MongoEngine中嵌套的嵌入式文档的验证错误

普拉萨德·奥斯特瓦尔

我正在创建具有两个级别的嵌套嵌入式文档(嵌入式文档内部的嵌入式文档)

这是代码:

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)(注释。在列表字段> 1.comments中只能使用列表和元组。在列表字段中只能使用列表和元组:['comments'])

我尝试使用单个嵌套文档,它也可以工作,如果我不使用EmbeddedDocuments作为列表,它也可以工作。但不确定为什么它不适用于嵌入式文档列表的多个级别。

挖泥船

问题来自以下两行:

comment1 = Comment(content='Good work!',comments=CommentDetails(name='John',category='fashion'))
comment2 = Comment(content='Nice article!',comments=CommentDetails(name='Mike',category='tech'))

comments 应该是一个列表,但是您提供了一个对象。

用这个:

comment1 = Comment(content='Good work!',comments=[CommentDetails(name='John',category='fashion')])
comment2 = Comment(content='Nice article!',comments=[CommentDetails(name='Mike',category='tech')])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章