Hello. I would like to have a Document with the list of integers, but I'd also need to accept None values there (e.g. [1, 2, 3, None, 4])
Is this currently possible with Mongoengine?
I expected the code below to be working, because there is an option to specify null=True in the inner field definition
```import mongoengine as me
class Doc(me.Document):
list_field = me.ListField(field=me.IntField(null=True))
```
Unfortunately, this throws the error when trying to convert None to int. Am I missing something? Is there another way to achieve this?
I did a video on the subject of the ListField and it covers this very specifically: https://www.youtube.com/watch?v=dl-oHfmdW50
But, in short, if the Listfield contains a specific type, then it is an array and does not support elements of a type that do not match.
However, you can use a generic field instead:
class Doc(me.Document):
list_field = me.ListField(me.DynamicField())
Now, you can freely mix null (None) and integers.
Btw, the null parameters purpose is...not well documented. It has more to do with default handling for missing fields. It does not allow for using None in the sense you are wanting.
@MateuszBelczowski , Did this answer resolve the issue? If so, please close it. Thanks!
@JohnAD Yes, thanks
This should be added to the docs somehow, took me quite a few hours to dig this issue up :(
Thanks for the answer!
Most helpful comment
@MateuszBelczowski , Did this answer resolve the issue? If so, please close it. Thanks!