1

我正在使用 yasg 库为我的 api 制作一个文档。但我有一个问题:GET 和 DELETE 方法很好,但是当我想使用 POST 或 PUT 方法时,我无法为它们定义有效负载。在参数部分它说:没有参数这是我的代码:

class CreateGroupView(APIView):
    def post(self, request):
        try:
            serializer = GroupSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status.HTTP_201_CREATED)
            else:
                return Response(status=status.HTTP_400_BAD_REQUEST)
        except:
            return Response({'data': 'somethings wrong'}, status.HTTP_500_INTERNAL_SERVER_ERROR)

我能做些什么?

4

1 回答 1

1
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

type_param = openapi.Parameter('type', in_=openapi.IN_QUERY, description='Type parameter', type=openapi.TYPE_INTEGER)
q_param = openapi.Parameter('q', in_=openapi.IN_QUERY, description='Seach query', type=openapi.TYPE_STRING)

class CreateGroupView(APIView):

    @swagger_auto_schema(
        responses={status.HTTP_201_CREATED: GroupSerializer()},
        operation_description="Some description here...",
        manual_parameters=[type_param, q_param]
    )
    def post(self, request):
        try:
            serializer = GroupSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status.HTTP_201_CREATED)
            else:
                return Response(status=status.HTTP_400_BAD_REQUEST)
        except:
            return Response({'data': 'somethings wrong'}, status.HTTP_500_INTERNAL_SERVER_ERROR)
于 2020-11-28T09:40:39.713 回答