Ben RR Ben RR - 1 month ago 5
Python Question

Django ForeignKey field required despite blank=True and null=True

I am using Django REST Framework and I have a

MyNodel
with a related
MyOtherModel
in a many-to-one relationship:

models.ForeignKey(MyModel, related_name="my_other_models", blank=True, null=True)


Although
blank=True, null=True
, when I try to post a
MyModel
JSON without a
my_other_models
field I get a "this field is required" error.

Answer

In your serializer, you need to add required=False.

field = MyModelSerializer(required=False)