From d9f5e5addbad5e1a01f67e7358e4f5091c3cad81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Sat, 15 Sep 2012 18:30:02 +0300 Subject: [PATCH] Reworked REQUIRED_FIELDS + create_user() interaction --- .../auth/management/commands/createsuperuser.py | 4 ++-- django/contrib/auth/models.py | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py index 9821b00c4d3..d8023428e17 100644 --- a/django/contrib/auth/management/commands/createsuperuser.py +++ b/django/contrib/auth/management/commands/createsuperuser.py @@ -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 diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index b9ba0193bac..473bf218ea9 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -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'),