Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #19596 -- Use `_default_manager` instead of `objects` in the au…

…th app.

This is needed to support custom user models which don't define a manager
named `objects`.
  • Loading branch information...
commit cc4de61a2b36abf418d6f4c720d9e62c405e0612 1 parent 456f9b9
@apollo13 apollo13 authored
View
4 django/contrib/auth/backends.py
@@ -13,7 +13,7 @@ class ModelBackend(object):
def authenticate(self, username=None, password=None):
try:
UserModel = get_user_model()
- user = UserModel.objects.get_by_natural_key(username)
+ user = UserModel._default_manager.get_by_natural_key(username)
if user.check_password(password):
return user
except UserModel.DoesNotExist:
@@ -64,7 +64,7 @@ def has_module_perms(self, user_obj, app_label):
def get_user(self, user_id):
try:
UserModel = get_user_model()
- return UserModel.objects.get(pk=user_id)
+ return UserModel._default_manager.get(pk=user_id)
except UserModel.DoesNotExist:
return None
View
4 django/contrib/auth/forms.py
@@ -89,7 +89,7 @@ def clean_username(self):
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"]
try:
- User.objects.get(username=username)
+ User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])
@@ -217,7 +217,7 @@ def clean_email(self):
"""
UserModel = get_user_model()
email = self.cleaned_data["email"]
- self.users_cache = UserModel.objects.filter(email__iexact=email)
+ self.users_cache = UserModel._default_manager.filter(email__iexact=email)
if not len(self.users_cache):
raise forms.ValidationError(self.error_messages['unknown'])
if not any(user.is_active for user in self.users_cache):
View
4 django/contrib/auth/handlers/modwsgi.py
@@ -18,7 +18,7 @@ def check_password(environ, username, password):
try:
try:
- user = UserModel.objects.get_by_natural_key(username)
+ user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
return None
if not user.is_active:
@@ -37,7 +37,7 @@ def groups_for_user(environ, username):
try:
try:
- user = UserModel.objects.get_by_natural_key(username)
+ user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
return []
try:
View
2  django/contrib/auth/management/__init__.py
@@ -174,7 +174,7 @@ def get_default_username(check_db=True):
# Don't return the default username if it is already taken.
if check_db and default_username:
try:
- auth_app.User.objects.get(username=default_username)
+ auth_app.User._default_manager.get(username=default_username)
except auth_app.User.DoesNotExist:
pass
else:
View
2  django/contrib/auth/management/commands/changepassword.py
@@ -33,7 +33,7 @@ def handle(self, *args, **options):
UserModel = get_user_model()
try:
- u = UserModel.objects.using(options.get('database')).get(**{
+ u = UserModel._default_manager.using(options.get('database')).get(**{
UserModel.USERNAME_FIELD: username
})
except UserModel.DoesNotExist:
View
4 django/contrib/auth/management/commands/createsuperuser.py
@@ -95,7 +95,7 @@ def handle(self, *args, **options):
username = None
continue
try:
- self.UserModel.objects.db_manager(database).get_by_natural_key(username)
+ self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
except self.UserModel.DoesNotExist:
pass
else:
@@ -134,6 +134,6 @@ def handle(self, *args, **options):
user_data[self.UserModel.USERNAME_FIELD] = username
user_data['password'] = password
- self.UserModel.objects.db_manager(database).create_superuser(**user_data)
+ self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
if verbosity >= 1:
self.stdout.write("Superuser created successfully.")
View
22 django/contrib/auth/tests/auth_backends.py
@@ -34,7 +34,7 @@ def tearDown(self):
ContentType.objects.clear_cache()
def test_has_perm(self):
- user = self.UserModel.objects.get(pk=self.user.pk)
+ user = self.UserModel._default_manager.get(pk=self.user.pk)
self.assertEqual(user.has_perm('auth.test'), False)
user.is_staff = True
user.save()
@@ -53,14 +53,14 @@ def test_has_perm(self):
self.assertEqual(user.has_perm('auth.test'), False)
def test_custom_perms(self):
- user = self.UserModel.objects.get(pk=self.user.pk)
+ user = self.UserModel._default_manager.get(pk=self.user.pk)
content_type = ContentType.objects.get_for_model(Group)
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
user.user_permissions.add(perm)
user.save()
# reloading user to purge the _perm_cache
- user = self.UserModel.objects.get(pk=self.user.pk)
+ user = self.UserModel._default_manager.get(pk=self.user.pk)
self.assertEqual(user.get_all_permissions() == set(['auth.test']), True)
self.assertEqual(user.get_group_permissions(), set([]))
self.assertEqual(user.has_module_perms('Group'), False)
@@ -71,7 +71,7 @@ def test_custom_perms(self):
perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3')
user.user_permissions.add(perm)
user.save()
- user = self.UserModel.objects.get(pk=self.user.pk)
+ user = self.UserModel._default_manager.get(pk=self.user.pk)
self.assertEqual(user.get_all_permissions(), set(['auth.test2', 'auth.test', 'auth.test3']))
self.assertEqual(user.has_perm('test'), False)
self.assertEqual(user.has_perm('auth.test'), True)
@@ -81,7 +81,7 @@ def test_custom_perms(self):
group.permissions.add(perm)
group.save()
user.groups.add(group)
- user = self.UserModel.objects.get(pk=self.user.pk)
+ user = self.UserModel._default_manager.get(pk=self.user.pk)
exp = set(['auth.test2', 'auth.test', 'auth.test3', 'auth.test_group'])
self.assertEqual(user.get_all_permissions(), exp)
self.assertEqual(user.get_group_permissions(), set(['auth.test_group']))
@@ -93,7 +93,7 @@ def test_custom_perms(self):
def test_has_no_object_perm(self):
"""Regressiontest for #12462"""
- user = self.UserModel.objects.get(pk=self.user.pk)
+ user = self.UserModel._default_manager.get(pk=self.user.pk)
content_type = ContentType.objects.get_for_model(Group)
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
user.user_permissions.add(perm)
@@ -106,7 +106,7 @@ def test_has_no_object_perm(self):
def test_get_all_superuser_permissions(self):
"A superuser has all permissions. Refs #14795"
- user = self.UserModel.objects.get(pk=self.superuser.pk)
+ user = self.UserModel._default_manager.get(pk=self.superuser.pk)
self.assertEqual(len(user.get_all_permissions()), len(Permission.objects.all()))
@@ -151,13 +151,13 @@ class ExtensionUserModelBackendTest(BaseModelBackendTest, TestCase):
UserModel = ExtensionUser
def create_users(self):
- self.user = ExtensionUser.objects.create_user(
+ self.user = ExtensionUser._default_manager.create_user(
username='test',
email='test@example.com',
password='test',
date_of_birth=date(2006, 4, 25)
)
- self.superuser = ExtensionUser.objects.create_superuser(
+ self.superuser = ExtensionUser._default_manager.create_superuser(
username='test2',
email='test2@example.com',
password='test',
@@ -178,12 +178,12 @@ class CustomPermissionsUserModelBackendTest(BaseModelBackendTest, TestCase):
UserModel = CustomPermissionsUser
def create_users(self):
- self.user = CustomPermissionsUser.objects.create_user(
+ self.user = CustomPermissionsUser._default_manager.create_user(
email='test@example.com',
password='test',
date_of_birth=date(2006, 4, 25)
)
- self.superuser = CustomPermissionsUser.objects.create_superuser(
+ self.superuser = CustomPermissionsUser._default_manager.create_superuser(
email='test2@example.com',
password='test',
date_of_birth=date(1976, 11, 8)
View
8 django/contrib/auth/tests/custom_user.py
@@ -42,7 +42,7 @@ class CustomUser(AbstractBaseUser):
is_admin = models.BooleanField(default=False)
date_of_birth = models.DateField()
- objects = CustomUserManager()
+ custom_objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth']
@@ -88,7 +88,7 @@ def is_staff(self):
class ExtensionUser(AbstractUser):
date_of_birth = models.DateField()
- objects = UserManager()
+ custom_objects = UserManager()
REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ['date_of_birth']
@@ -112,7 +112,7 @@ class CustomPermissionsUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
date_of_birth = models.DateField()
- objects = CustomPermissionsUserManager()
+ custom_objects = CustomPermissionsUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth']
@@ -136,7 +136,7 @@ class IsActiveTestUser1(AbstractBaseUser):
"""
username = models.CharField(max_length=30, unique=True)
- objects = BaseUserManager()
+ custom_objects = BaseUserManager()
USERNAME_FIELD = 'username'
View
2  django/contrib/auth/tests/handlers.py
@@ -42,7 +42,7 @@ def test_check_password_custom_user(self):
with custom user installed
"""
- CustomUser.objects.create_user('test@example.com', '1990-01-01', 'test')
+ CustomUser._default_manager.create_user('test@example.com', '1990-01-01', 'test')
# User not in database
self.assertTrue(check_password({}, 'unknown', '') is None)
View
6 django/contrib/auth/tests/management.py
@@ -125,7 +125,7 @@ def test_email_in_username(self):
email="joe@somewhere.org",
stdout=new_io
)
- u = User.objects.get(username="joe+admin@somewhere.org")
+ u = User._default_manager.get(username="joe+admin@somewhere.org")
self.assertEqual(u.email, 'joe@somewhere.org')
self.assertFalse(u.has_usable_password())
@@ -145,7 +145,7 @@ def test_swappable_user(self):
)
command_output = new_io.getvalue().strip()
self.assertEqual(command_output, 'Superuser created successfully.')
- u = CustomUser.objects.get(email="joe@somewhere.org")
+ u = CustomUser._default_manager.get(email="joe@somewhere.org")
self.assertEqual(u.date_of_birth, date(1976, 4, 1))
# created password should be unusable
@@ -167,7 +167,7 @@ def test_swappable_user_missing_required_field(self):
skip_validation=True
)
- self.assertEqual(CustomUser.objects.count(), 0)
+ self.assertEqual(CustomUser._default_manager.count(), 0)
class PermissionDuplicationTestCase(TestCase):
View
2  django/contrib/auth/tests/models.py
@@ -137,6 +137,6 @@ def test_is_active_field_default(self):
user.is_active = False
# there should be no problem saving - but the attribute is not saved
user.save()
- user_fetched = UserModel.objects.get(pk=user.pk)
+ user_fetched = UserModel._default_manager.get(pk=user.pk)
# the attribute is always true for newly retrieved instance
self.assertEqual(user_fetched.is_active, True)
View
2  django/contrib/auth/views.py
@@ -200,7 +200,7 @@ def password_reset_confirm(request, uidb36=None, token=None,
post_reset_redirect = reverse('django.contrib.auth.views.password_reset_complete')
try:
uid_int = base36_to_int(uidb36)
- user = UserModel.objects.get(pk=uid_int)
+ user = UserModel._default_manager.get(pk=uid_int)
except (ValueError, OverflowError, UserModel.DoesNotExist):
user = None
Please sign in to comment.
Something went wrong with that request. Please try again.