Search code examples
pythondjangopermissionsdjango-rest-frameworkurl-parameters

How can I get an access to url paramaters in django rest framework permission class?


I have a view:

class DealsView(APIView):
    permission_classes = (IsAuthenticated, IsOwnerOrCuratorOrDirectorOrNotAllowed, )

    def get(self, request, user_pk):
        ...

But in order to check the permissions correctly I need to pass user_pk url argument to permission:

class IsOwnerOrCuratorOrDirectorOrNotAllowed(permissions.BasePermission):
    def has_permission(self, request, view):
        ...

By default it doesn't have any arguments except self, request, and view. How can I go through it?


Solution

  • Try this:

    class IsOwnerOrCuratorOrDirectorOrNotAllowed(permissions.BasePermission):
        def has_permission(self, request, view):
            user_pk = view.kwargs.get('user_pk', None)
            ...