我有一个视图,它在向端点发送POST请求时创建一个post。
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)
1条答案
按热度按时间2izufjch1#
响应没有关键字参数
errors
,而是使用数据,因为serializer.errors
只是一个JSON/字典:在最后一行代码中: