Reworked REQUIRED_FIELDS + create_user() interaction

This commit is contained in:
Anssi Kääriäinen 2012-09-15 18:30:02 +03:00
parent 579f152e4a
commit d9f5e5addb
2 changed files with 8 additions and 6 deletions

View File

@ -29,7 +29,7 @@ class Command(BaseCommand):
) + tuple(
make_option('--%s' % field, dest=field, default=None,
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.'
@ -43,7 +43,7 @@ class Command(BaseCommand):
UserModel = get_user_model()
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
password = None

View File

@ -170,7 +170,7 @@ class BaseUserManager(models.Manager):
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.
"""
@ -180,14 +180,14 @@ class UserManager(BaseUserManager):
email = UserManager.normalize_email(email)
user = self.model(username=username, email=email,
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.save(using=self._db)
return user
def create_superuser(self, username, email, password):
u = self.create_user(username, email, password)
def create_superuser(self, username, email, password, **extra_fields):
u = self.create_user(username, email, password, **extra_fields)
u.is_staff = True
u.is_active = True
u.is_superuser = True
@ -228,6 +228,7 @@ def _user_has_module_perms(user, app_label):
class AbstractBaseUser(models.Model):
REQUIRED_FIELDS = []
password = models.CharField(_('password'), max_length=128)
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.
"""
REQUIRED_FIELDS = ['email']
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and '
'@/./+/-/_ characters'),