WrappedAttributeError'IsAdminUser'对象没有属性'authenticate'

时间:2018-11-17 14:19:07

标签: django django-rest-framework django-authentication

我正在将Django Rest Framework与permission_classes一起使用来创建API。我的理解是,一旦我以未经身份验证的用户身份访问该API,就应该重定向到登录表单,对吗?相反,我收到以下错误:

WrappedAttributeError at /sessions/api/listPatients/ 'IsAdminUser' object has no attribute 'authenticate'

没有重定向到登录表单...而且我不明白为什么。

为什么会这样?我究竟做错了什么?如何解决?

这里是urls.py

path('api/getSessions/',
     views.GetSessions.as_view(),
     name="GetSessionsAPI"),

这是我要尝试的视图:

class GetSessions(generics.ListCreateAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = SessionSerializer

    def get_queryset(self):
        if self.request.user.is_authenticated:
            return Session.objects.filter(
                patient__created_by_user=self.request.user)
        else:
            raise PermissionDenied

这是另一个引发错误的错误:

class ListPatients(generics.ListAPIView):

    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = PatientsSerializer

    def get_queryset(self):
        if self.request.user.is_authenticated:
            try:
                return Patient.objects.filter(
                    created_by_user=self.request.user).\
                   filter(curently_active=True)
            except Patient.DoesNotExist:
                raise PermissionDenied
    else:
        raise PermissionDenied

这是完整的追溯:

WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
Request Method: GET
Request URL:    http://localhost:8000/sessions/api/listPatients/
Django Version: 2.0.5
Exception Type: WrappedAttributeError
Exception Value:    
'IsAdminUser' object has no attribute 'authenticate'
Exception Location: /Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate, line 375
Python Executable:  /Users/karol/Development/ZS_2_0/ZS_2_0_venv/bin/python
Python Version: 3.6.4
Python Path:    
['/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
 '/Users/karol/Development/ZS_2_0',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/patient',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/services',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/notes',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/practice',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/activity_logger',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/my_calendar',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/session/tests',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0/users',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
 '/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python36.zip',
 '/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6',
 '/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages',
 '/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0',
 '/Users/karol/Development/ZS_2_0/zapiszsesje_2_0/zapiszsesje_2_0']
Server time:    Sat, 17 Nov 2018 15:29:56 +0100
Traceback Switch to copy-and-paste view

/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
        yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
                self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
                user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars
During handling of the above exception ('IsAdminUser' object has no attribute 'authenticate'), another exception occurred:
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/exception.py in inner
            response = get_response(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in inner
                return func(*args, **kwds) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/decorators/csrf.py in wrapped_view
        return view_func(*args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/views/generic/base.py in view
            return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
            response = self.handle_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in handle_exception
            self.raise_uncaught_exception(exc) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
            self.initial(request, *args, **kwargs) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in initial
        self.perform_authentication(request) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/views.py in perform_authentication
        request.user ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
                self._authenticate() ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__
                self.gen.throw(type, value, traceback) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
        six.reraise(type(exc), exc, info[2]) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/django/utils/six.py in reraise
            raise value.with_traceback(tb) ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in wrap_attributeerrors
        yield ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in user
                self._authenticate() ...
▶ Local vars
/Users/karol/Development/ZS_2_0/ZS_2_0_venv/lib/python3.6/site-packages/rest_framework/request.py in _authenticate
                user_auth_tuple = authenticator.authenticate(self) ...
▶ Local vars

更新: 当我更改base.py设置文件中的以下行时,此错误消失:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.permissions.IsAdminUser', # <- commenting out solves the problem!
    ),

但是,该行已在Two Scoops of Django ...

之后添加

2 个答案:

答案 0 :(得分:1)

事实证明,由于我的问题下的评论,我的错误才被发现:

由于错误,我在设置文件中将'rest_framework.permissions.IsAdminUser',添加到了DEFAULT_AUTHENTICATION_CLASSES而不是DEFAULT_PERMISSION_CLASSES中。

那解决了问题-谢谢! :)

答案 1 :(得分:0)

当您创建身份验证模型时,您的视图集类中需要 TokenAuthentication。 例如:

class LessonViewSet(viewsets.ModelViewSet):
    queryset = Lesson.objects.all()
    serializer_class = LessonSerializer
    authentication_classes = (TokenAuthentication)
    premission_classes = (IsAuthenticated, )