leedjango
leedjango

Reputation: 411

How to customize response message in ModelViewSet

I will send the request in post format using ModelViewSet and I will customize its response message. So I turned the response back on the perform_create method as shown in the following code, but it doesn't work as I want.

class CreateReadPostView (ModelViewSet) :
    serializer_class = PostSerializer
    permission_classes = [IsAuthenticated]
    queryset = Post.objects.all()
    pagination_class = LargeResultsSetPagination

    def perform_create (self, serializer) :
        serializer.save(author=self.request.user)
        return Response({'success': '게시물이 저장 되었습니다.'}, status=201) # it's not work

How can I make this work normally? Thank in advance.

Upvotes: 1

Views: 1039

Answers (1)

JPG
JPG

Reputation: 88549

Override the create(...) method

class CreateReadPostView(ModelViewSet):
    serializer_class = PostSerializer
    permission_classes = [IsAuthenticated]
    queryset = Post.objects.all()
    pagination_class = LargeResultsSetPagination

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

    def create(self, request, *args, **kwargs):
        super().create(request, *args, **kwargs)
        return Response({'success': '게시물이 저장 되었습니다.'}, status=201)

Upvotes: 3

Related Questions