While using DRF APIView with drf_yasg appears additional POST query, that i have not defined

25 views Asked by At

I have got this urls

urlpatterns = [
    path('users/', UserApiView.as_view()),
    path('users/<str:pk>/', UserApiView.as_view()),
]

and APIView class:


class UserApiView(APIView):

    @swagger_auto_schema(
        request_body=CreateUserSerializer,
        responses={'200': openapi.Response('response description', DetailUserSerializer(many=True))}
    )
    def post(self, request):
        user_data = CreateUserSerializer(request.data)
        user_data.is_valid(raise_exception=True)

        new_user = user_data.save()
        return Response(DetailUserSerializer(new_user).data)

    @swagger_auto_schema(
        responses={'200': openapi.Response('response description', DetailUserSerializer)}
    )
    def get(self, request):
        queryset = UserRepo().get_all()
        serializer = DetailUserSerializer(queryset, many=True)
        return Response({'data': serializer.data})

    @swagger_auto_schema(
        responses={'200': openapi.Response('response description',               DetailUserSerializer(many=True))}
    )
    def get(self, request, pk=None):
        queryset = UserRepo().get_all()
        user = get_object_or_404(queryset, pk=pk)

        serializer = DetailUserSerializer(user)
        return Response(serializer.data)

but in swagger i see extra /users/{id}/ POST query (it's the last one)

enter image description here

I tried to find something in drf_yasg docs, but it was not successful

1

There are 1 answers

0
Alexandr Sokolov On BEST ANSWER

I solved this problem dividing UserApiView to several different classes. One of them handles users/, another one /users/pk so, no more redundant docs