如何解决如何让 Django rest 框架尊重我的序列化程序中的“required=false”指定?
我正在使用 djangorestframework==3.12.2 。我有这个序列化器。有些字段,比如“desc_english”,我不想被要求......
class ValidateNewCoopSerializer(serializers.Serializer):
coop_name=serializers.CharField()
street=serializers.CharField()
address_public=serializers.CharField()
city=serializers.CharField()
state=serializers.CharField()
zip=serializers.CharField()
county=serializers.CharField()
country=serializers.CharField()
websites=serializers.CharField()
contact_name=serializers.CharField()
contact_name_public=serializers.CharField()
contact_email=serializers.CharField()
contact_email_public=serializers.CharField()
contact_phone=serializers.CharField()
contact_phone_public=serializers.CharField()
scope=serializers.CharField()
tags=serializers.CharField(required=False)
desc_english=serializers.CharField(required=False)
desc_other=serializers.CharField(required=False)
req_reason=serializers.CharField()
在我的 views.py 文件中像这样使用
@api_view(('POST',))
def save_to_sheet_from_form(request):
"""
This is supposed to write to a Google sheet given a form coming from
the client.
"""
valid_ser = ValidateNewCoopSerializer(data=request.data)
if valid_ser.is_valid():
post_data = valid_ser.validated_data
...
return Response(post_data,status=status.HTTP_201_CREATED)
else:
return Response(valid_ser.errors,status=status.HTTP_400_BAD_REQUEST)
不过,我注意到,当我提交请求时,标记为不需要的字段会返回错误......
curl 'http://localhost:8000/save_to_sheet_from_form/' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: http://localhost:3001/' -H 'Content-Type: application/json' -H 'Origin: http://localhost:3001' -H 'Connection: keep-alive' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' --data-raw '{"coop_name":"","street":"","address_public":"no","city":"","state":"IL","zip":"","county":"","country":"US","websites":"","contact_name":"","contact_name_public":"no","contact_email":"","contact_email_public":"no","contact_phone":"","contact_phone_public":"no","scope":"local","tags":"","desc_english":"","desc_other":"","req_reason":"add"}'
结果
{"coop_name":["This field may not be blank."],"street":["This field may not be blank."],"city":["This field may not be blank."],"zip":["This field may not be blank."],"county":["This field may not be blank."],"websites":["This field may not be blank."],"contact_name":["This field may not be blank."],"contact_email":["This field may not be blank."],"contact_phone":["This field may not be blank."],"tags":["This field may not be blank."],"desc_english":["This field may not be blank."],"desc_other":["This field may not be blank."]}
如何配置序列化程序以使这些字段实际上不是必需的?
解决方法
您已在序列化程序字段上设置 required=False
。这意味着,如果您不在请求中为他们提供密钥,它会起作用,但是如果您确实提供了密钥,那么他们会 > 得到验证。查看您通过 "desc_english":""
的请求,即您确实为密钥提供了一个空字符串,因此该错误是正确的。如果您想允许空字符串,您可以在字段上设置 allow_blank=True
[DRF docs]:
desc_english=serializers.CharField(required=False,allow_blank=True)
如果您可能想将空值(python 中的 None
)传递给字段,您还可以将 allow_null
[DRF docs] 设置为 True
:
desc_english=serializers.CharField(required=False,allow_blank=True,allow_null=True)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。