'MongoEngine with Flask - Error 'NoneType' object has no attribute 'choices'

I'm trying to get MongoEngine with the Flask-Mongoengine extension working, however whenever I use a ListField I get the error below:

if field.field.choices:
    AttributeError: 'NoneType' object has no attribute 'choices'

Here is my code:

class Business(db.Document):
    name = db.StringField(required=True)
    address = db.StringField()
    location = db.GeoPointField()
    tags = db.ListField()
    area = db.ReferenceField(Area, dbref=True)
    contact = db.EmbeddedDocumentField(Contact)
    details = db.EmbeddedDocumentField(details)


Solution 1:[1]

I had the same problem. What fixed it for me was passing a Field object to the ListField() call in the ListField declarations, example:

tags = db.ListField(db.StringField())

Solution 2:[2]

I faced the same issue but for the DictField while update. I was trying to save file name as lable and the details of the as its value object e.g. The class structure is

class Folder(Document):
    name = StringField(required=True)
    fileList = DictField()
class File(EmbeddedDocument):
    name = StringField(required=True)
    size = IntField()
 ...
 fld = Folder(name="fld1")
 fl = File(name="fl1.txt",size=10)
 fld.fileList["fl1.txt"] = fl

It was working while save but failing when I tried to update the record.

After couple of permutation, I realized that "." in lable used for assigning file object to fileList was problem. Below change in the last line of code worked

fld.fileList["fl1_txt"] = fl

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergio Pulgarin
Solution 2 skvp