Exclude URLs from Django REST Swagger

后端 未结 6 2140
再見小時候
再見小時候 2020-12-31 05:27

I have a few URLs that I want to exclude from my REST API documentation. I\'m using Django REST Swagger and the only documentation I can find (https://github.com/marcgibbons

6条回答
  •  抹茶落季
    2020-12-31 05:49

    Ola's answer is correct. exclude_namespaces is no longer supported.

    For finer control of the documentation, create your own schema view by using a function-based or class-based view. This can be useful if you want to produce documentation for specific URL patterns, or URL confs.

    In your views.py, you can do the following:

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.schemas import SchemaGenerator
    from rest_framework_swagger import renderers
    
    class SwaggerSchemaView(APIView):
        renderer_classes = [
            renderers.OpenAPIRenderer,
            renderers.SwaggerUIRenderer
        ]
    
        def get(self, request):
            generator = SchemaGenerator(title='Your API Documentation', urlconf='your_app.urls')
            schema = generator.get_schema(request=request)
    
        return Response(schema)
    

    The above will only render documentation for the URLs that are specified in the urlconf argument of the SchemaGenerator. Also, don't forget to set up your urls.py as well:

    from django.conf.urls import url
    from views import SwaggerSchemaView
    
    urlpatterns = [
        url(r'^api/v1/docs/$', SwaggerSchemaView.as_view(), name='docs'),
    ]
    

提交回复
热议问题