Django-rest-framework: add extra permission to the ViewSet update method

I have the following code:

class UsersViewSet(viewsets.ModelViewSet):
    model = Users
    permission_classes = (IsAuthenticated,)

    def update(self, request, *args, **kwargs):
        return super(UsersViewSet, self).update(request, *args, **kwargs)

The question arises:

  • How to add additional permission only for update method? (must get isAuthenticated + Permission)
  • overwrite permissions only for update method? (you need to get only permission without isAuthenticated) other methods in the viewet must have IsAuthenticated permission.

Is it possible to do this with a decorator? Or something else?

Want to get something like this:

@permission_classes((IsAuthenticated, AdditionalPermission ))
def update:
    pass

But if I write this code, the second permission will not be verified through the request

+4
source share
4 answers

LAST EDITING

, DRF- ( , ), , :

def get_permissions(self):
    # Your logic should be all here
    if self.request.method == 'GET':
        self.permission_classes = [DummyPermission, ]
    else:
        self.permission_classes = [IsAuthenticated, ]

    return super(UsersViewSet, self).get_permissions()

, , . , , .

, ( ). :

@permission_classes([IsAuthenticated, AdditionalPermission, ])
def update:
    pass

:

?

, , DRF (, ), - ( allow_classes - , ) - ( @permission_classes). , , :

@permission_classes([AdditionalPermission, ])
def update:
    pass

ISAuthenticated , .

?

, (), . :

  • AdditionalPermission, , .

.

+13

get_permissions():

class MyViewSet(viewsets.ModelViewSet):

    def get_permissions(self):
        if self.action in ('update', 'other_viewset_method'):
            self.permission_classes = [permissions.CustomPermissions,]
        return super(self.__class__, self).get_permissions()
+7

@permission_classes . @detail_route (permission_classes = (permissions.CustomPermissions,)) , .

, :

 class MyViewSet(viewsets.ModelViewSet):

    def update(self, request, *args, **kwargs):
        self.methods=('put',)
        self.permission_classes = (permissions.CustomPermissions,)
        return super(self.__class__, self).update(request, *args, **kwargs)

. drf v3.1.1

+2

All Articles