How can I exclude a view from the API documentation?

I am using django-rest-framework (DRF) to provide my site's REST API. As far as I understand, the automatic generation of API documentation is based on the generation of DRF schema . It can then be interpreted by a third-party tool to create good documentation. I used it django-rest-swaggeras a tool that creates beautiful documentation from a DRF schema, but I suspect that the solution to my problem is based on DRF, no matter which tool I use to convert the schema to something “beautiful”.

The problem is that by default, DRF provides documentation for the entire API, although some parts of it do indeed for my sites have an internal purpose. I would like some descriptions to be excluded from the documentation. Some submissions should always be excluded. Some submissions should be excluded only if the user viewing the documentation does not enter the site.

+4
source share
1 answer

This is poorly documented. In a class-based view, you can set the attribute exclude_from_schemavalue Trueto exclude the view from the documentation. Example:

from rest_framework import viewsets, mixins
class SomeViewSet(mixins...., viewsets.GenericViewSet):
    exclude_from_schema = True

    [...]

exclude_from_schema, , @api_view decorator, , . DRF.

, @property, exclude_from_schema . , DRF , Request, , . , , , , :

from rest_framework import viewsets, mixins, permissions
class SomeViewSet(mixins...., viewsets.GenericViewSet):
    @property
    def exclude_from_schema(self):
        return not permissions.IsAuthenticated().has_permission(self.request,
                                                                self)

    [...]

self.request - Request, , . , rest_framework.permissions , , , permission_classes, SomeViewSet.

+7

Source: https://habr.com/ru/post/1666369/


All Articles