I have the following in my views.py
I've been reading the documentation and trying to work out is there a way to exclude certain endpoints for this view.
from EoX.models import Vendor
from rest_framework import viewsets
from rest_framework import permissions
from .serializers import VendorSerializer
from url_filter.integrations.drf import DjangoFilterBackend
from drf_yasg.utils import swagger_auto_schema
from django.utils.decorators import method_decorator
class VendorViewSet(viewsets.ModelViewSet):
queryset = Vendor.objects.all()
serializer_class = VendorSerializer
permission_classes = [permissions.AllowAny]
filter_backends = [DjangoFilterBackend]
filter_fields = ['name',]
I can exclude the view all together with
schema=None
I tried decorating it with
@method_decorator(name='list', decorator=swagger_auto_schema(
operation_description="description from swagger_auto_schema via method_decorator"
))
and that changes the description of the 'get' endpoint.
but trying
@method_decorator(name='list', decorator=swagger_auto_schema(methods=['get'],
operation_description="description from swagger_auto_schema via method_decorator"
))
throws an error
AssertionError:methodormethodscan only be specified on @action or @api_view views
Is this a bug? or just not a supported feature, and is there a way to accomplish what I am trying?
Many thanks.
Could you please specify what you are looking for?
Do you a) want to exclude the list endpoint or b) change the swagger description of it or c) something else?
For case a) see here. Just provide the auto_schema=None in your first mentioned setup and you should be fine.
Case b) - you just did this with the setup but for the LIST-Endpoint (GET HTTP-verb on a url ending like /my_resources/). If how ever you want to modify the description of the retrieve-endpoint (GET HTTP-verb on a url ending like /my_resources/{id}) you need to change name=list to name=retrieve.
Hi, What I am trying to do is restrict my endpoints to the GET method. From the documentation it states that I should use @method_decorator
'If you want to customize the generation of a method you are not implementing yourself, you can use swagger_auto_schema in combination with Django鈥檚 method_decorator:'
However using the method_decorator I can not successfully restrict the endpoints to 'GET' only.
I can change the description as per your example, but I'm trying to restrict the Endpoints for the VendorViewSet to 'GET' methods only.
Thank you
You can use @method_decorator(name='create', decorator=swagger_auto_schema(auto_schema=None)) on the methods you want to hide.
If you want to do this more genrically, you can use a SchemaGenerator or SwaggerAutoSchema subclass, overriding the should_include_endpoint to return False, or the get_operation method to return None, respectively, according to the method parameter.
Most helpful comment
You can use
@method_decorator(name='create', decorator=swagger_auto_schema(auto_schema=None))on the methods you want to hide.If you want to do this more genrically, you can use a SchemaGenerator or SwaggerAutoSchema subclass, overriding the
should_include_endpointto returnFalse, or theget_operationmethod to returnNone, respectively, according to themethodparameter.