question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'ObjectPermissionBackend' object has no attribute 'get_user'

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
orfcommented, Nov 28, 2016

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.

0reactions
blueyedcommented, Dec 2, 2016

Awesome, it got fixed in Django: https://github.com/django/django/commit/47744a0a4ed0b9e2d3f52de65abcf6cef9a14e31. Let’s close it here then.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found