does MongoEngine supports having different data types in a list? For example, I'd like a ListField()
to store IntField()
as well as StringField()
. Is there a way to do this in MongoEngine?
The ListField
does not enforce a datatype unless you ask it to. However if you do then it has to be a single datatype at the moment. For example
This works:
import mongoengine as mdb
class Stuff(mdb.Document):
things = mdb.ListField()
s = Stuff(things=['1',2,[4,5]])
s.save()
this throws TypeError
as it is enforcing a datatype:
import mongoengine as mdb
class Stuff(mdb.Document):
things = mdb.ListField(mdb.IntField())
s = Stuff(things=['1',2,[4,5]])
s.save()
this throws AttributeError
as it is expecting a Field as the first argument:
import mongoengine as mdb
class Stuff(mdb.Document):
things = mdb.ListField([mdb.IntField(),mdb.StringField(),mdb.ListField()])
s = Stuff(things=['1',2,[4,5]])
s.save()
I can see the final example being a useful so you might want to file an issue on the project repo.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.