django 当使用DRF APIView和drf_yasg时,会出现额外的POST查询,但我没有定义

col17t5w  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(103)

我有这个网址

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

APIView类:

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)

但在swagger中,我看到了额外的/users/{id}/ POST查询(这是最后一个)
enter image description here
我试图在drf_yasg docs中找到一些东西,但没有成功

tv6aics1

tv6aics11#

我解决了这个问题,将UserApiView划分为几个不同的类。其中一个处理users/,另一个处理/users/pk,所以没有多余的文档

相关问题