简体   繁体   English

Django DRF:@permission_classes 不起作用

[英]Django DRF: @permission_classes not working

I have a view with a custom action which should have a custom permission "IsRightUser".我有一个自定义操作的视图,该操作应该具有自定义权限“IsRightUser”。 However, the has_object_permission of it is never called, even though I try to access the object with self.get_object() in my view.但是,它的has_object_permission从未被调用,即使我在我看来尝试使用self.get_object()访问该对象。

class MyView(mixins.ListModelMixin, viewsets.GenericViewSet):
    serializer_class = MySerializer
    lookup_field = 'uuid'
    queryset = MyObject.objects.all()

    @action(methods=['get'], detail=True)
    @permission_classes([IsRightUser])
    def groups(self, request, uuid=None):
        # prints [<class 'rest_framework.permissions.IsAuthenticated'>]
        print(self.permission_classes)  
        my_object = self.get_object()
        groups = Group.objects.filter(my_object=my_object)
        serializer = MySerializer(groups, many=True)
        return Response(serializer.data)

Here you can see my custom permission which is never called.在这里您可以看到我从未调用过的自定义权限。

class IsRightUser(BasePermission):
    def has_object_permission(self, request, view, obj):
        # never called
        return True

When I use permission_classes = [IsRightUser] in my view (ie directly underneath the lookup_field) it works (unfortunately this is not feasible for me).当我在我的视图中使用permission_classes = [IsRightUser]时(即直接在lookup_field 下方)它可以工作(不幸的是这对我来说不可行)。

Any help is very much appreciated.很感谢任何形式的帮助。

You should pass permission classes as action argument directly:您应该直接将权限类作为action参数传递:

@action(methods=['get'], detail=True, permission_classes=[IsRightUser])
def groups(self, request, uuid=None):
    # prints [<class 'rest_framework.permissions.IsAuthenticated'>]
    print(self.permission_classes)  
    my_object = self.get_object()
    groups = Group.objects.filter(my_object=my_object)
    serializer = MySerializer(groups, many=True)
    return Response(serializer.data)

例如,只要您在 REST_FRAMEWORK 下将默认 DEFAULT_AUTHENTICATION_CLASSES 定义到 settings.py 中,第一个装饰器就会完美地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django DRF @permission_classes 不适用于 IsAdminUser 权限 - Django DRF @permission_classes not working for IsAdminUser permission Django Rest 框架权限类不工作 - Django Rest Framework permission_classes not working 用于访问路由的 Django permission_classes 不可调用 - Django permission_classes for access to route is not callable ViewSet方法的Django休息框架permission_classes - Django rest framework permission_classes of ViewSet method 我是 Django 新手,正在做一个项目。 在我的项目中,我需要使用不同的“permission_classes”从多个地方调用相同的 API - I am new to Django and working on a project. In my project I need to call a same API from multiple places with different "permission_classes" django rest框架filter_backends类在Permission_classes类之前调用 - django rest framework filter_backends class called before permission_classes class 无法从分配 permission_classes 切换到使用装饰器 @permission_classes - Can not switch from assigning permission_classes to use decorator @permission_classes 如何使用相同的URL为GET和POST请求设置不同的Permission_class? - How to set different permission_classes for GET and POST requests using the same URL? 如何为 DRF 中的方法设置不同的权限类 - How to have different permission classes for methods in DRF 如何在 DRF 的 DEFAULT_PERMISSION_CLASSES 中使用逻辑运算符? - How to use logical operators in DRF's DEFAULT_PERMISSION_CLASSES?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM