How to solve "user.backend = backend AttributeError: 'NoneType' object has no attribute 'backend" in Django Tests

I'm new to django 3.6 and tried to write a test that involved the use of force_login. But anytime I run it, it gives me the error user.backend = backend AttributeError: 'NoneType' object has no attribute 'backend' even when my force_login backend parameter is already set to django.contrib.auth.backends.ModelBackend

this is my test code

def test_address_create_stores_user(self):
        user3 = models.User.objects.create_user('user3', 'pw432joij')
        post_data = {
            'name':'john kercher',
            'address1':'1 av st',
            'address2':'',
            'zip_code':'MA12GS',
            'city':'Manchester',
            'country':'uk',
            }
 
        self.client.force_login(user3, backend='django.contrib.auth.backends.ModelBackend')
        self.client.post(reverse('address_create', post_data))
        self.assertTrue(models.Address.objects.filter(user=user3).exists())

and this is the error it gives

*ERROR: test_address_create_stores_user (main.tests.tests_views.TestPage)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\DD\Desktop\practical django\booktime\main\tests\tests_views.py", line 128, in test_address_create_stores_user
    self.client.force_login(user3, backend='hhh')
  File "C:\Users\DD\.virtualenvs\practical_django-UtJdiNPl\lib\site-packages\django\test\client.py", line 600, in force_login
    user.backend = backend
AttributeError: 'NoneType' object has no attribute 'backend'*

I see your question is from sept 2022, so don't know if you figured this out or even still need this answered.

I was having a similar issue. Your question made me double check something in my code and I solved it on my end. By extension, I think I have an idea of what your issue likely is (it's impossible to tell without seeing more of your code base). I'll answer your question in two parts:


  1. Something that you seem to think is a problem (but most likely isn't) is the auth backend. By default, Django specifies django.contrib.auth.backends.ModelBackend as the default auth backend. Thus this will automatically propagate to your testing client. In other words, providing this to the force_login() method is not necessary.

Sidenote: If you ever find yourself asking "is the default auth backend good for my project?", then the answer is almost certainly "yes". In my experience, you'll know when the default backend is inadequate, because there will specific login criteria/conditions that it cannot meet for a given usecase. For a vast majority of use cases though, the default backend is perfectly fine.


  1. The actual problem (most likely) is that self.client.force_login() expects a proper User model as the first arg. The error is even fairly directly stating that it's receiving a "NoneType object". Aka, the None keyword, aka a null variable.

I can't say for certain without seeing further code. But I would guess that, somewhere, your project is adjusting the create_user() function. And most likely, this overridden function is not returning a proper user object. It might be as simple as forgetting the return statement. Or maybe the logic to create a user is broken. I have no idea.

If I'm correct, then if you were print out user3 to console before you do anything with it, it will most likely print out None. Correct this and the force_login() function should work.

Back to Top