AttributeError When Overwritting AUTH_USER_MODEL in Test Cases

32 views
Skip to first unread message

Aubrey Stark-Toller

unread,
Feb 8, 2016, 9:20:07 AM2/8/16
to django...@googlegroups.com
Hello,

When I override the AUTH_USER_MODEL setting to "auth.User", when
AUTH_USER_MODEL in settings is not set to "auth.User", in
either a TestCase or individual test, and then try to access the objects
attribute on the user model, I get an AttributeError.

The exact error I get is:
AttributeError: Manager isn't available; 'auth.User' has been swapped
for 'None'

I've found this to be the case in both Django 1.8 and 1.9.

This is easily reproducible in a fresh project : create a boilerplate
project with a boilerplate app, add a new user model (call it
CustomUser) to the app and set to AUTH_USER_MODEL to CustomUser, and add
the following test case to the app:

> from django.test import TestCase, override_settings
> from django.contrib.auth import get_user_model
>
> class MyTestCase(TestCase):
> @override_settings(AUTH_USER_MODEL = 'auth.User')
> def test_custom_user(self):
> UserModel = get_user_model()
> UserModel.objects.all()

get_user_model() retrieves the correct model but accessing objects
attribute gives the stated error when running the test.

If I throw in another user model (say CustomerUser2) and write a test
such as:

> @override_settings(AUTH_USER_MODEL = 'another_app.CustomerUser2')
> def test_custom_user(self):
> UserModel = get_user_model()
> UserModel.objects.all()

this works fine, and if I unset AUTH_USER_MODEL in settings again
everything works as expected.

Perhaps someone can shed some light on this behavior?

Cheers,
Aubrey

sarfaraz ahmed

unread,
Oct 19, 2017, 4:43:04 AM10/19/17
to Django users
I am getting similar issue. No reply on your question. Please share the solution if you found
Reply all
Reply to author
Forward
0 new messages