Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit actions in django rest framework

I'm using viewsets that has several actions (retrieve, list, create etc...). I also use swagger to get a clean overview of my API. The problem is that it's full of unused method (PATCH, PUT, DELETE) and it messes up the view.

I've tried to do this in my viewsets : allowed_methods = ('GET','POST',)

The swagger still has all these unused method. How can I change this behavior ? Is there another way to limit the number of actions in a viewset ? Or maybe the problem is on swagger side ?

like image 224
Ambroise Collon Avatar asked Feb 09 '23 15:02

Ambroise Collon


1 Answers

You need to compose your viewset view more accurately to get rid of them.

Default ModelViewSet is:

class ModelViewSet(
        mixins.CreateModelMixin,
        mixins.RetrieveModelMixin,
        mixins.UpdateModelMixin,
        mixins.DestroyModelMixin,
        mixins.ListModelMixin,
        GenericViewSet):
    pass

Therefore if you just want say the list and create methods, it'll be:

class MyViewSet(
        mixins.CreateModelMixin,
        mixins.ListModelMixin,
        GenericViewSet):
    serializer_class = ....
    queryset = ....
like image 93
Linovia Avatar answered Feb 15 '23 10:02

Linovia