如何在django rest框架权限类中访问url参数?

时间:2017-11-20 17:03:48

标签: python django permissions django-rest-framework url-parameters

我有一个观点:

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

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

但是为了正确检查权限,我需要将user_pk url参数传递给权限:

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

默认情况下,它没有除self,request和view之外的任何参数。我该怎么办呢?

1 个答案:

答案 0 :(得分:4)

试试这个:

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