Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListField(DictField()) with mongoengine

How is it possible to ListField(DictField()) with mongoengine and access it, because the below code does not work?

from mongoengine import *

class Test():
    g = ListField(DictField(Mapping.build(
    test1=StringField(required=True),
    test2=StringField(required=True)
)))
like image 683
user977828 Avatar asked Mar 21 '26 08:03

user977828


2 Answers

I recognize that this post is very old, but for anyone finding this thread starting on using mongoengine. To improve on Niranj's answer, there now exists an EmbeddedDocumentListField and you need to inherit from EmbeddedDocument or Document in those classes.

class classEmbed(EmbeddedDocument):
    t = StringField()
    p = StringField()

class Test(Document):
    g = EmbeddedDocumentListField(classEmbed)

The documentation is here under Fields

like image 125
Ben Matase Avatar answered Mar 23 '26 06:03

Ben Matase


Try using this format,

class classEmbed:
     t = StringField()
     p = StringField()

class Test:
     g = ListField(EmbeddedDocumentField(classEmbed))
like image 30
Niranj Rajasekaran Avatar answered Mar 23 '26 07:03

Niranj Rajasekaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!