Arka-cell
Arka-cell

Reputation: 906

How to remove put method in RetrieveUpdateAPIView from drf-spectacular API documentation?

I have the following view:

class PersonalInfos(generics.RetrieveUpdateAPIView):
    serializer_class = ClientSerializer
    permission_classes = [IsAuthenticated]
    def get_queryset(self):
        """
        :return: A QuerySet Object
        """
        return Client.objects.get(user=self.request.user)

    def get(self, *args):
        """
        :param args: Handled by rest_framework views.dispatch

        :return: JSON object containing User Personal Data
        """
        queryset = self.get_queryset()
        serializer = ClientSerializer(queryset)
        return Response(data=serializer.data)

    def patch(self, request):
        """
        :param request: request object is sent by the client

        :return:  Json response with the data sent of the body
        """

        client = self.get_queryset()
        serializer = ClientSerializer(client, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(data=serializer.data, status=200)
        return Response(data="Unexpected Parameters", status=400)

Everything works fine in the view, but the problem is that I am using drf-spectacular and it is showing me a PUT method in the documentation that we won't be needing in the API. My questions is, how can I customize drf-spectacular to not include a PUT method in the documentation?

enter image description here

Upvotes: 2

Views: 2836

Answers (3)

stephapp
stephapp

Reputation: 33

this should exclude the put method in drf-yasg

class MyView(generics.RetrieveUpdateAPIView):

    @swagger_auto_schema(auto_schema=None)
    def put(self, request, *args, **kwargs):
        return

source: https://drf-yasg.readthedocs.io/en/stable/custom_spec.html#excluding-endpoints

Upvotes: 1

Abhilash Meluveettil
Abhilash Meluveettil

Reputation: 181

You may use the @extend_schema decorator to exclude one or more methods from the schema generated, as shown below.

@extend_schema(methods=['PUT'], exclude=True)

Upvotes: 10

Arka-cell
Arka-cell

Reputation: 906

I solved this using RetrieveAPIView instead of UpdateRetrieveAPIView and I have extended it with to include a PATCH method. RetrieveAPIView will handle a PATCH method perfectly and would not show automatically an UPDATE request in the API documentation. Here is the new code:

class PersonalInfos(generics.RetrieveAPIView):
    serializer_class = ClientSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        """
        :return: A QuerySet Object
        """
        return Client.objects.get(user=self.request.user)

    def get(self, *args):
        """
        :param args: Handled by rest_framework views.dispatch

        :return: JSON object containing User Personal Data
        """
        queryset = self.get_queryset()
        serializer = ClientSerializer(queryset)
        return Response(data=serializer.data)

    def patch(self, request):
        """
        :param request: request object is sent by the client

        :return:  Json response with the data sent of the body
        """

        client = self.get_queryset()
        serializer = ClientSerializer(client, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(data=serializer.data, status=200)
        return Response(data="Unexpected Parameters", status=400)

Upvotes: 0

Related Questions