Issue
I'm using Django Rest Framework with permission_classes
to create an API. My understanding is that once I go to that API with an unauthenticated user I should be redirected to the login form, right? Instead, I get the following error:
WrappedAttributeError at /sessions/api/listPatients/
'IsAdminUser' object has no attribute 'authenticate'
There's no redirect to the login form... and I don't understand why.
Why is this happening? What am I doing wrong? How to fix that?
Here's the urls.py
:
path('api/getSessions/',
views.GetSessions.as_view(),
name="GetSessionsAPI"),
And here's the view that I'm trying to go to:
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
and here's the other one raising the error:
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
Here's the full traceback:
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
UPDATE: This error goes away when I change the following line in my base.py settings file:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.permissions.IsAdminUser', # <- commenting out solves the problem!
),
However, the line has been added following the book Two Scoops of Django ...
Solution
Turned out it was my mistake which got uncovered thanks to the comments under my question:
By mistake I have added 'rest_framework.permissions.IsAdminUser',
to DEFAULT_AUTHENTICATION_CLASSES
instead of DEFAULT_PERMISSION_CLASSES
inside my settings file.
That solved the problem - thanks! :)
Answered By - user1544500
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.