问题
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.
回答1:
In your serializer, you need to add required=False
.
field = MyModelSerializer(required=False)
来源:https://stackoverflow.com/questions/40237969/django-foreignkey-field-required-despite-blank-true-and-null-true