Alexander Shpindler
Alexander Shpindler

Reputation: 951

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?

Upvotes: 4

Views: 2968

Answers (1)

origamic
origamic

Reputation: 1116

Try this:

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

Upvotes: 19

Related Questions