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

Validation Error on nested Embedded Documents in 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) (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 作为列表它也有效。但不确定为什么它不适用于多个级别的嵌入式文档列表。

问题出在这两行:

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')])