代码之家  ›  专栏  ›  技术社区  ›  Devin Dixon

访问路由的django权限类不可调用

  •  0
  • Devin Dixon  · 技术社区  · 6 年前

    我正在尝试将权限更改为特定的路由。我想打开一些路由,我想验证用户的其他路由。我的代码如下:

    from rest_framework.decorators import api_view, permission_classes
    from rest_framework.permissions import IsAuthenticated
    from rest_framework.viewsets import GenericViewSet
    
    class UserViewSet(GenericViewSet):
          queryset = User.objects.all()
          serializer_class = UserSerializer
          permission_classes = [IsAuthenticated]
          renderer_classes = [JSONRenderer]
    
    
        @action(url_path="an/api/path", detail=False, methods=["post"], renderer_classes=[JSONRenderer])
        @api_view(['GET'])
        @permission_classes((IsAuthenticated, ))
        def get_stuff(self, request):
            #Will get stuff
    

    但我一直有个错误:

     File "/code/api/views/UserViewSet.py", line 16, in <module>
    api_1     |     class UserViewSet(GenericViewSet):
    api_1     |   File "/code/api/views/UserViewSet.py", line 33, in UserViewSet
    api_1     |     @permission_classes((IsAuthenticated, ))
    api_1     | TypeError: 'list' object is not callable
    

    在我的settings.py中,我有:

    REST_FRAMEWORK = {
        "DEFAULT_PERMISSION_CLASSES": [
            "rest_framework.permissions.IsAuthenticated",
        ],
        "DEFAULT_AUTHENTICATION_CLASSES": [
            "rest_framework_jwt.authentication.JSONWebTokenAuthentication",
            "rest_framework.authentication.SessionAuthentication",
            "rest_framework.authentication.BasicAuthentication",
        ],
        "DEFAULT_RENDERER_CLASSES": (
            "rest_framework.renderers.JSONRenderer",
        )
    

    你知道为什么它总是抛出list“object is not callable function”吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   chamoda    6 年前

    去除 permission_classes = [IsAuthenticated] . 它覆盖了decorator。

    class UserViewSet(GenericViewSet):
          queryset = User.objects.all()
          serializer_class = UserSerializer
          renderer_classes = [JSONRenderer]
    
    
        @action(url_path="an/api/path", detail=False, methods=["post"], renderer_classes=[JSONRenderer])
        @api_view(['GET'])
        @permission_classes((IsAuthenticated, ))
        def get_stuff(self, request):
            #Will get stuff