diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 26b5394aa..602d90fcd 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -483,15 +483,40 @@ def test_with_live_server(live_server): def test_custom_user_model(django_testdir, username_field): django_testdir.create_app_file( """ - from django.contrib.auth.models import AbstractUser + from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin from django.db import models - class MyCustomUser(AbstractUser): + class MyCustomUserManager(BaseUserManager): + def create_user(self, {username_field}, password=None, **extra_fields): + extra_fields.setdefault('is_staff', False) + extra_fields.setdefault('is_superuser', False) + user = self.model({username_field}={username_field}, **extra_fields) + user.set_password(password) + user.save() + return user + + def create_superuser(self, {username_field}, password=None, **extra_fields): + extra_fields.setdefault('is_staff', True) + extra_fields.setdefault('is_superuser', True) + return self.create_user( + {username_field}={username_field}, + password=password, + **extra_fields + ) + + class MyCustomUser(AbstractBaseUser, PermissionsMixin): + email = models.EmailField(max_length=100, unique=True) identifier = models.CharField(unique=True, max_length=100) + is_staff = models.BooleanField( + 'staff status', + default=False, + help_text='Designates whether the user can log into this admin site.' + ) - USERNAME_FIELD = '%s' - """ - % (username_field), + objects = MyCustomUserManager() + + USERNAME_FIELD = '{username_field}' + """.format(username_field=username_field), "models.py", ) django_testdir.create_app_file( @@ -551,19 +576,13 @@ class Migration(migrations.Migration): ('password', models.CharField(max_length=128, verbose_name='password')), ('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)), ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), - ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, max_length=30, validators=[django.core.validators.RegexValidator(r'^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.', 'invalid')], help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', unique=True, verbose_name='username')), - ('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)), - ('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)), - ('email', models.EmailField(max_length=254, verbose_name='email address', blank=True)), + ('email', models.EmailField(error_messages={'unique': 'A user with that email address already exists.'}, max_length=100, unique=True, verbose_name='email address')), ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), - ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), - ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), ('identifier', models.CharField(unique=True, max_length=100)), ('groups', models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Group', blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', verbose_name='groups')), ('user_permissions', models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Permission', blank=True, help_text='Specific permissions for this user.', verbose_name='user permissions')), ], options={ - 'abstract': False, 'verbose_name': 'user', 'verbose_name_plural': 'users', },