Reworked REQUIRED_FIELDS + create_user() interaction
This commit is contained in:
parent
579f152e4a
commit
d9f5e5addb
|
@ -29,7 +29,7 @@ class Command(BaseCommand):
|
||||||
) + tuple(
|
) + tuple(
|
||||||
make_option('--%s' % field, dest=field, default=None,
|
make_option('--%s' % field, dest=field, default=None,
|
||||||
help='Specifies the %s for the superuser.' % field)
|
help='Specifies the %s for the superuser.' % field)
|
||||||
for field in getattr(get_user_model(), 'REQUIRED_FIELDS', ['email'])
|
for field in get_user_model().REQUIRED_FIELDS
|
||||||
)
|
)
|
||||||
|
|
||||||
help = 'Used to create a superuser.'
|
help = 'Used to create a superuser.'
|
||||||
|
@ -43,7 +43,7 @@ class Command(BaseCommand):
|
||||||
UserModel = get_user_model()
|
UserModel = get_user_model()
|
||||||
|
|
||||||
username_field = UserModel._meta.get_field(getattr(UserModel, 'USERNAME_FIELD', 'username'))
|
username_field = UserModel._meta.get_field(getattr(UserModel, 'USERNAME_FIELD', 'username'))
|
||||||
other_fields = getattr(UserModel, 'REQUIRED_FIELDS', ['email'])
|
other_fields = UserModel.REQUIRED_FIELDS
|
||||||
|
|
||||||
# If not provided, create the user with an unusable password
|
# If not provided, create the user with an unusable password
|
||||||
password = None
|
password = None
|
||||||
|
|
|
@ -170,7 +170,7 @@ class BaseUserManager(models.Manager):
|
||||||
|
|
||||||
class UserManager(BaseUserManager):
|
class UserManager(BaseUserManager):
|
||||||
|
|
||||||
def create_user(self, username, email=None, password=None):
|
def create_user(self, username, email=None, password=None, **extra_fields):
|
||||||
"""
|
"""
|
||||||
Creates and saves a User with the given username, email and password.
|
Creates and saves a User with the given username, email and password.
|
||||||
"""
|
"""
|
||||||
|
@ -180,14 +180,14 @@ class UserManager(BaseUserManager):
|
||||||
email = UserManager.normalize_email(email)
|
email = UserManager.normalize_email(email)
|
||||||
user = self.model(username=username, email=email,
|
user = self.model(username=username, email=email,
|
||||||
is_staff=False, is_active=True, is_superuser=False,
|
is_staff=False, is_active=True, is_superuser=False,
|
||||||
last_login=now, date_joined=now)
|
last_login=now, date_joined=now, **extra_fields)
|
||||||
|
|
||||||
user.set_password(password)
|
user.set_password(password)
|
||||||
user.save(using=self._db)
|
user.save(using=self._db)
|
||||||
return user
|
return user
|
||||||
|
|
||||||
def create_superuser(self, username, email, password):
|
def create_superuser(self, username, email, password, **extra_fields):
|
||||||
u = self.create_user(username, email, password)
|
u = self.create_user(username, email, password, **extra_fields)
|
||||||
u.is_staff = True
|
u.is_staff = True
|
||||||
u.is_active = True
|
u.is_active = True
|
||||||
u.is_superuser = True
|
u.is_superuser = True
|
||||||
|
@ -228,6 +228,7 @@ def _user_has_module_perms(user, app_label):
|
||||||
|
|
||||||
|
|
||||||
class AbstractBaseUser(models.Model):
|
class AbstractBaseUser(models.Model):
|
||||||
|
REQUIRED_FIELDS = []
|
||||||
password = models.CharField(_('password'), max_length=128)
|
password = models.CharField(_('password'), max_length=128)
|
||||||
last_login = models.DateTimeField(_('last login'), default=timezone.now)
|
last_login = models.DateTimeField(_('last login'), default=timezone.now)
|
||||||
|
|
||||||
|
@ -283,6 +284,7 @@ class User(AbstractBaseUser):
|
||||||
|
|
||||||
Username and password are required. Other fields are optional.
|
Username and password are required. Other fields are optional.
|
||||||
"""
|
"""
|
||||||
|
REQUIRED_FIELDS = ['email']
|
||||||
username = models.CharField(_('username'), max_length=30, unique=True,
|
username = models.CharField(_('username'), max_length=30, unique=True,
|
||||||
help_text=_('Required. 30 characters or fewer. Letters, numbers and '
|
help_text=_('Required. 30 characters or fewer. Letters, numbers and '
|
||||||
'@/./+/-/_ characters'),
|
'@/./+/-/_ characters'),
|
||||||
|
|
Loading…
Reference in New Issue