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():
serializer.save(author=user)
return Response(data=serializer.data, status=status.HTTP_201_CREATED)
return Response(errors=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
CodePudding user response:
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)