How to set multiple permissions in one class view, depending on http request

django, django-permissions, django-rest-framework, django-views

Solution

Just an update.

You can override 'get_permissions' instead of 'get_queryset'.

For example:

def get_permissions(self):
    if self.request.method == 'GET':
        return [permissions.AllowAny()]
    elif self.request.method == 'DELETE':
        return [permissions.IsAdminUser()]
    else:  # PUT, PATCH
        return [...]

Note that 'get_permission' returns a list of permission instances, not classes.

Problem

I am working with django-rest-framework. The problem I am having is that the url is identical for both the POST and the GET methods but I want to have different permissions depending on which method is being called. Right now I'm using class based views and I can't figure out how to set different permissions depending on the method. What I want is if the user is an admin that they both POST and GET, if the user is authenticated than they can only GET, and if the user isn't authenticated they can't do anything. ``` class CategoryList(generics.ListCreateAPIView): queryset = QuestionCategory.objects.all() serializer_class = QuestionCategorySerializer permission_classes = (permissions.IsAuthenticated,) ```

Original source