I get TypeError: Response.__init__() got an unexpected keyword argument 'errors' when trying to send POST request

I have this view that creates a post when sending a POST request to the endpoint.

    class PostViewSet(viewsets.ModelViewSet):
          serializer_class = PostSerializer
          queryset = Post.objects.all()
          permission_classes = [IsAuthorOrReadOnly]
          def create(self, request, *args, **kwargs):
               serializer = self.get_serializer(data=request.data)
                user = request.user
                if serializer.is_valid():
                     return Response(data=serializer.data, status=status.HTTP_201_CREATED)
                return Response(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Response doesn't have a keyword argument of errors. Instead, just use data since serializer.errors is just a JSON/dictionary:

In your last line of code:

return Response(serializer.errors, status=status. HTTP_400_BAD_REQUEST)
Back to Top