Thursday, October 19, 2017

Re: AttributeError When Overwritting AUTH_USER_MODEL in Test Cases

I am getting similar issue. No reply on your question. Please share the solution if you found


On Monday, 8 February 2016 19:50:07 UTC+5:30, Aubrey Stark-Toller wrote:
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20fc529a-8452-40f6-8a6e-e8a11cdc2bd1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment