Django Rest Swagger APIView

后端 未结 2 1379
臣服心动
臣服心动 2020-12-30 09:20

I made an API and want to make swagger doc. I don\'t develop any Serializes for that.

Views.py

class DeliveryView(APIVie         


        
2条回答
  •  天涯浪人
    2020-12-30 09:37

    Consider using the GenericAPIView instead as this will generate the documentation. It is a bit of a hack using this when the endpoint does not relate to a model, but it does work.

    As an example, the following code will create an endpoint that only accepts post requests and is documented within swagger using the seralizer.

    class SomeThing(GenericAPIView):
        serializer_class = MySerializer
    
        def post(self, request, *args, **kwargs):
            serializer = MySerializer(data=request.data)
            if serializer.is_valid() is False:
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    
            res = do_magic(**serializer.data)
            return Response(res)
    

提交回复
热议问题