Skip to content

Commit

Permalink
Improved error handling and docs for get_user_model()
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Sep 23, 2012
1 parent 0229209 commit 98aba85
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion django/contrib/auth/__init__.py
Expand Up @@ -101,7 +101,10 @@ def get_user_model():
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
except ValueError:
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
return get_model(app_label, model_name)
user_model = get_model(app_label, model_name)
if user_model is None:
raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
return user_model


def get_user(request):
Expand Down
25 changes: 25 additions & 0 deletions django/contrib/auth/tests/basic.py
@@ -1,10 +1,14 @@
import locale

from django.contrib.auth import get_user_model
from django.contrib.auth.management.commands import createsuperuser
from django.contrib.auth.models import User, AnonymousUser
from django.contrib.auth.tests.custom_user import CustomUser
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.six import StringIO


Expand Down Expand Up @@ -149,3 +153,24 @@ class mock_getpass:
# If we were successful, a user should have been created
u = User.objects.get(username="nolocale@somewhere.org")
self.assertEqual(u.email, 'nolocale@somewhere.org')

def test_get_user_model(self):
"The current user model can be retrieved"
self.assertEqual(get_user_model(), User)

@override_settings(AUTH_USER_MODEL='auth.CustomUser')
def test_swappable_user(self):
"The current user model can be swapped out for another"
self.assertEqual(get_user_model(), CustomUser)

@override_settings(AUTH_USER_MODEL='badsetting')
def test_swappable_user_bad_setting(self):
"The alternate user setting must point to something in the format app.model"
with self.assertRaises(ImproperlyConfigured):
get_user_model()

@override_settings(AUTH_USER_MODEL='thismodel.doesntexist')
def test_swappable_user_nonexistent_model(self):
"The current user model must point to an installed model"
with self.assertRaises(ImproperlyConfigured):
get_user_model()
2 changes: 1 addition & 1 deletion docs/topics/auth.txt
Expand Up @@ -1791,7 +1791,7 @@ different User model.

Instead of referring to :class:`~django.contrib.auth.models.User` directly,
you should reference the user model using
:meth:`~django.contrib.auth.get_user_model()`. This method will return the
:func:`django.contrib.auth.get_user_model()`. This method will return the
currently active User model -- the custom User model if one is specified, or
:class:`~django.contrib.auth.User` otherwise.

Expand Down

0 comments on commit 98aba85

Please sign in to comment.