In this blog post, we'll explore how to hide specific APIs from the Swagger documentation in Django REST. Swagger is a useful tool for generating documentation for REST APIs, but sometimes you may want to exclude certain APIs from the documentation for security or other reasons.
There are two main ways to hide APIs from Swagger in Django REST:
- Use
swagger_schema = None
The swagger_schema
attribute can be set to None
to exclude a view from schema generation. This can be done in the view class itself or in a decorator. For example, in the following code, the get
method of the MyView
class is excluded from schema generation:
from drf_yasg.utils import swagger_auto_schema class MyView(APIView): @swagger_auto_schema(auto_schema=None) def get(self, request): pass
- Use a decorator
Alternatively, you can use the @swagger_auto_schema
decorator to exclude a view from schema generation. The following code is equivalent to the previous example:
from drf_yasg.utils import swagger_auto_schema @swagger_auto_schema(auto_schema=None) def get(self, request): pass
Both of these methods will effectively hide the specified API from the Swagger documentation.