Home » Python » python – Validation Error on nested Embedded Documents in MongoEngine-Exceptionshub

python – Validation Error on nested Embedded Documents in MongoEngine-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I’m creating nested Embedded documents with two levels(Embedded Document inside Embedded Document)

Here’s the code:

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()

It gives following error on running:

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

I tried with single nested document and it works,also If I don’t use EmbeddedDocuments for list it works. but not sure why it is not working for multiple levels of list of Embedded Documents .

How to&Answers: