Drf-yasg: Is there a way to exclude viewset or explicitly include viewset ?

Created on 21 Sep 2019  路  4Comments  路  Source: axnsan12/drf-yasg

There seems to be an option for ApiView
swagger_schema = None

I'd like to prevent some endpoints being generated (it's just too many of them)

Most helpful comment

Hello,

As mentioned in the docs (https://drf-yasg.readthedocs.io/en/stable/custom_spec.html#excluding-endpoints), you'd have to decorate the excluded endpoints with swagger_auto_schema and pass it auto_schema=None.

Example:

class MyViewSet(viewsets.ModelViewSet):

    @swagger_auto_schema(auto_schema=None)
    def list(self, request, *args, **kwargs):
        # ....

This way, the get operation on your viewset list will not be output in the resulting swagger.

All 4 comments

Hello,

As mentioned in the docs (https://drf-yasg.readthedocs.io/en/stable/custom_spec.html#excluding-endpoints), you'd have to decorate the excluded endpoints with swagger_auto_schema and pass it auto_schema=None.

Example:

class MyViewSet(viewsets.ModelViewSet):

    @swagger_auto_schema(auto_schema=None)
    def list(self, request, *args, **kwargs):
        # ....

This way, the get operation on your viewset list will not be output in the resulting swagger.

Oh great, is it possible to exclude entire viewset?

You could decorate all of your viewset's methods the same way to exclude them all (you can use Django's method_decorator as described in swagger_auto_schema's docs).

If this seems a bit tedious, I think you can also

I haven't tried that solution, but I guess it would be cleaner to avoid decorating every method for every unwanted viewset.

For me, using schema = None works fine:

class SomeViewSet(GenericViewSet):
    schema = None
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Amoki picture Amoki  路  4Comments

Safrone picture Safrone  路  3Comments

csdenboer picture csdenboer  路  3Comments

gertjanol picture gertjanol  路  5Comments

hnykda picture hnykda  路  3Comments