Alternative for cumbersome ListField(EmbeddedDocumentField) in MongoEngine¶
In MongoEngine, we often need to store a list of embedded documents in one document. The traditional way is to use ListField(EmbeddedDocumentField(document_type))
, which is a bit cumbersome.
Alternative¶
EmbeddedDocumentListField
can make things easier. Here's an example.
Before:
from mongoengine import Document, ListField, EmbeddedDocumentField
class Post(Document):
comments = ListField(EmbeddedDocumentField(Comment))
After:
from mongoengine import Document, EmbeddedDocumentListField
class Post(Document):
comments = EmbeddedDocumentListField(Comment)
This article is originally created by tooli.top. Please indicate the source when reprinting : https://www.tooli.top/posts/mongoengine_list
Posted on 2023-02-22
Mail to author