AttributeError: 'ObjectPermissionBackend' object has no attribute 'get_user'
See original GitHub issueI am trying to use Django’s Client.force_login
during tests, which appears to trigger an AttributeError
when using ObjectPermissionBackend
in settings.AUTHENTICATION_BACKENDS
:
[25] …/app/tests/test_middleware.py(23)test_ddt_middleware_normal()
-> response = client.get('/api/', HTTP_ACCEPT='text/html')
[26] …/Vcs/django/django/test/client.py(531)get()
-> **extra)
[27] …/Vcs/django/django/test/client.py(333)get()
-> return self.generic('GET', path, secure=secure, **r)
[28] …/Vcs/django/django/test/client.py(409)generic()
-> return self.request(**r)
[29] …/Vcs/django/django/test/client.py(478)request()
-> response = self.handler(environ)
[30] …/Vcs/django/django/utils/six.py(686)reraise()
-> raise value
[31] …/Vcs/django/django/core/handlers/exception.py(39)inner()
-> response = get_response(request)
[32] …/app/middleware.py(37)__call__()
-> if not (request.user and
[33] …/Vcs/django/django/utils/functional.py(234)inner()
-> self._setup()
[34] …/Vcs/django/django/utils/functional.py(380)_setup()
-> self._wrapped = self._setupfunc()
[35] …/Vcs/django/django/contrib/auth/middleware.py(24)<lambda>()
-> request.user = SimpleLazyObject(lambda: get_user(request))
[36] …/Vcs/django/django/contrib/auth/middleware.py(12)get_user()
-> request._cached_user = auth.get_user(request)
[37] > …/Vcs/django/django/contrib/auth/__init__.py(187)get_user()
-> user = backend.get_user(user_id)
The pytest test looks like this:
def test_foobar(db, client, some_user, some_group):
some_user.groups.add(some_group)
some_user.save()
client.force_login(some_user)
Setting a password and using login
works:
def test_foobar(db, client, some_user, some_group):
some_user.groups.add(some_group)
some_user.set_password('password')
some_user.save()
assert client.login(username=some_user.username,
password='password')
According to the documentation the get_user
method is required:
https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#writing-an-authentication-backend.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
'LoginForm' object has no attribute 'get_user' - Stack Overflow
The LoginView works with the assumption that you are using django.contrib.auth.forms.AuthenticationForm as the form which has a get_user ...
Read more >Loginform object has no attribute get_user : r/django - Reddit
I have multiuser app mith usertype a and b i am trying to create login form using class based views making my most...
Read more >"AttributeError: '__proxy__' object has no attribute 'decode ...
Fixed #25424 -- Use force_str for test client URLs. urlparse() fails with an AttributeError ("'proxy' object has no attribute 'decode'") if reverse_lazy is...
Read more >django.db models Example Code - Full Stack Python
StackedInline): pass ## ... source file continues with no further models ... store validated JSON in a Django object-relational mapper (ORM) database model....
Read more >'User' object has no attribute 'profile' - Django 1.9 error
Django : Django 1.9 error - 'User' object has no attribute 'profile' [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve made a ticket on the Django bug tracker: https://code.djangoproject.com/ticket/27542#ticket
PR #47 adds a note to the readme about this.
Awesome, it got fixed in Django: https://github.com/django/django/commit/47744a0a4ed0b9e2d3f52de65abcf6cef9a14e31. Let’s close it here then.