Django 休息框架 POST 请求:/competition-create 处的 JSONDecodeError

Django rest framework POST request: JSONDecodeError at /competition-create

我试图简单地执行一个 post 请求,将新的比赛保存到数据库中。

# views.py

class CompetitionCreateView(generics.CreateAPIView):
    serializer_class = CompetitionSerializer
    queryset = Competition.objects.all()

    def create(self, request, *args, **kwargs):
        serializer = CompetitionSerializer(data=request.data)
        if serializer.is_valid():
            competition = serializer.save()

            return Response(
                data=CompetitionSerializer(competition).data,
                status=status.HTTP_201_CREATED,
                content_type="json")
        return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST, content_type="json")

# serializer.py



class CompetitionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Competition
        fields = "__all__"

回复:

我试过使用常规 APIView 并从 def create(...) 切换到 def post(...) 但没有成功。还尝试将请求数据内容解析为json.

这是来自控制台的回溯

Internal Server Error: /competition-create
test_deploy_web | Traceback (most recent call last):
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
test_deploy_web |     response = get_response(request)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
test_deploy_web |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
test_deploy_web |     return view_func(*args, **kwargs)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
test_deploy_web |     return self.dispatch(request, *args, **kwargs)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
test_deploy_web |     response = self.handle_exception(exc)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
test_deploy_web |     self.raise_uncaught_exception(exc)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
test_deploy_web |     raise exc
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
test_deploy_web |     response = handler(request, *args, **kwargs)
test_deploy_web |   File "/app/view/views.py", line 21, in post
test_deploy_web |     if serializer.is_valid():
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 220, in is_valid
test_deploy_web |     self._validated_data = self.run_validation(self.initial_data)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 419, in run_validation
test_deploy_web |     value = self.to_internal_value(data)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 476, in to_internal_value
test_deploy_web |     validated_value = field.run_validation(primitive_value)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py", line 568, in run_validation
test_deploy_web |     value = self.to_internal_value(data)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py", line 1899, in to_internal_value
test_deploy_web |     return self.model_field.to_python(data)
test_deploy_web |   File "/usr/local/lib/python3.9/site-packages/django/contrib/postgres/fields/ranges.py", line 80, in to_python
test_deploy_web |     vals = json.loads(value)
test_deploy_web |   File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads
test_deploy_web |     return _default_decoder.decode(s)
test_deploy_web |   File "/usr/local/lib/python3.9/json/decoder.py", line 340, in decode
test_deploy_web |     raise JSONDecodeError("Extra data", s, end)
test_deploy_web | json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)

实际上你正在做一些有线连接的事情。所以不要使用这个:

# here you are serializing the already serialized data
return Response(
                data=CompetitionSerializer(competition).data,
                status=status.HTTP_201_CREATED,
                content_type="json")

试试这个:

return Response(
                data=serializer.data,
                status=status.HTTP_201_CREATED,
                content_type="json")

希望这能解决您的问题;)

输入 JSON 没有被正确解析是一个问题。