Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何让Django rest框架尊重;所需=假";在我的序列化程序中指定?_Python 3.x_Django_Django Rest Framework_Django Serializer_Required - Fatal编程技术网

Python 3.x 如何让Django rest框架尊重;所需=假";在我的序列化程序中指定?

Python 3.x 如何让Django rest框架尊重;所需=假";在我的序列化程序中指定?,python-3.x,django,django-rest-framework,django-serializer,required,Python 3.x,Django,Django Rest Framework,Django Serializer,Required,我使用的是djangorestframework==3.12.2。我有这个序列化程序。有些字段,比如“desc_english”,我不想被要求 class ValidateNewCoopSerializer(serializers.Serializer): coop_name=serializers.CharField() street=serializers.CharField() address_public=serializers.CharField() c

我使用的是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()
在my 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”:“
,即,您确实为密钥提供了一个空字符串,因此该错误是正确的。如果要允许空字符串,可以在字段上设置:

desc_english=serializers.CharField(required=False, allow_blank=True)
如果希望将空值(
None
在python中)传递给字段,也可以设置为
True

desc_english=serializers.CharField(required=False, allow_blank=True, allow_null=True)
谢谢我觉得“required=false”应该表示根本没有验证检查,但我猜“required”对不同的人意味着不同的东西。