如何让 Django 休息框架尊重我的序列化程序中的 "required=false" 指定?
How do I get Django rest framework to respect the "required=false" designation in my serializer?
我正在使用 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)
我正在使用 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)