Django Rest Framework: How to enable swagger docs for function based views

前端 未结 3 498
忘了有多久
忘了有多久 2021-01-17 09:10

I went through Django REST Swagger 2.1.2 documentation. When I tried with class based views, it was working fine.

But i did not find any reference on how to enable s

3条回答
  •  太阳男子
    2021-01-17 09:42

    i am not fammiliar with swagger,but you may try to use the decorator in this way:

    class TestView(View):
        @api_view(['GET', 'POST'])
        def get(self, request):
            ....
    

    or

    from django.utils.decorators import method_decorator
    class TestView(View):
        @method_decorator(api_view(['GET', 'POST'])
        def get(self, request):
            ....
    

    ----------------------------------------------------------------------------

    sorry, maybe i have misunderstood your question. according to the document, if you want to enable swagger in class based view. there is example:

    from rest_framework.permissions import AllowAny
    from rest_framework.response import Response
    from rest_framework.schemas import SchemaGenerator
    from rest_framework.views import APIView
    from rest_framework_swagger import renderers
    
    
    class SwaggerSchemaView(APIView):
        permission_classes = [AllowAny]
        renderer_classes = [
            renderers.OpenAPIRenderer,
            renderers.SwaggerUIRenderer
        ]
    
        def get(self, request):
            generator = SchemaGenerator()
            schema = generator.get_schema(request=request)
            return Response(schema)
    

    restframework will use these two renderer_classes to render Json and UI.

提交回复
热议问题