This code implements a custom schema view called 'ApiOnlySchemaView' that extends the base 'get_schema_view' function from Django REST Framework (DRF). The main goal is to filter API paths and display only those that have the '/api/' prefix in their path. This ensures that the API documentation focuses on the relevant endpoints, making it more manageable and user-friendly.

Here's a breakdown of the code:

  1. ApiOnlySchemaView Class:

    • get_schema(self, request): This method overrides the base get_schema method. It fetches the schema from the parent class and then filters the paths, keeping only those that contain '/api/' in their path. This ensures that only the API paths are displayed in the documentation.
    • get_ui_settings(self): This method (optional) allows you to customize the Swagger UI settings. In this example, it enables deep linking, persistent authorization, display of request duration, and sets the doc expansion to 'none'.
  2. schema_view:

    • The schema_view variable is used to create an instance of the custom ApiOnlySchemaView. It takes the Open API info object (providing metadata about the API) as an argument, sets public to True (allowing access to anyone), and defines the permission class to be permissions.AllowAny.

In essence, this code snippet showcases a streamlined approach to manage API documentation in Django REST Framework, providing a tailored and user-friendly experience for developers working with the API.

Note: This code assumes that the API endpoints follow the convention of having '/api/' as part of their path. You can adjust the filtering logic based on your specific API structure.