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

我只是一个虾纸丫 提交于 2021-01-04 05:31:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!