I've setup a REST api with django rest framework, using mongoengine as the ORM for my models. However, I keep getting this response back from the api for a field that should be optional:
{"ref":["This field may not be null."]}
The problem I'm running into is that it seems like all fields (I'm specifically using ReferenceFields
, but I've tried it with StringFields
, etc too) are not allowed to be optional/null.
I've tried setting validation methods to an empty lambda (that returns True
), setting null=True
and required=False
where the field is defined in the model.
from mongoengine import * class B(Document): ... class A(Document): ref = ReferenceField('B', null=True, required=False, dbref=False, validation=lambda: True)
I even tried explicitly setting the serializer in A's serializer so that I could tell it to allow nulls (with allow_null=True
):
from api.models import A,B from rest_framework_mongoengine.serializers import DocumentSerializer class BSerializer(DocumentSerializer): class Meta: model = B depth = 2 class ASerializer(DocumentSerializer): ref = BSerializer(allow_null=True) class Meta: model = A depth = 2
How do I get optional (nullable) fields to work with django rest framework and mongoengine? Again, this isn't just ReferenceFields
, it's the same with any field I try.