Django用于访问路由的Permission_classes不可调用

时间:2019-01-30 13:59:06

标签: python django django-rest-framework

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

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",
    )

有人知道为什么它不断抛出列表对象不是可调用函数吗?

1 个答案:

答案 0 :(得分:0)

删除permission_classes = [IsAuthenticated]。它会覆盖装饰器。

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