Issue with createsuperuser when implementing custom user model
Asked Answered
O

3

12

I am trying to implement my own custom user model in Django 1.6 but I am getting this error.

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 141, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() missing 1 required positional argument: 'email'

Here is my UserManager

class UserManager(BaseUserManager):

  def _create_user(self, username, email, password, is_staff, is_superuser, **extra_fields):
    now = timezone.now()
    email = self.normalize_email(email)
    user = self.model(username=username, email=email,
             is_staff=is_staff, is_active=False,
             is_superuser=is_superuser, last_login=now,
             date_joined=now, **extra_fields)
    user.set_password(password)
    user.save(using=self._db)
    return user

  def create_user(self, username, email=None, password=None, **extra_fields):
    return self._create_user(username, email, password, False, False,
                 **extra_fields)

  def create_superuser(self, username, email, password, **extra_fields):
    user=self._create_user(username, email, password, True, True,
                 **extra_fields)
    user.is_active=True
    user.save(using=self._db)
    return user

It seems like this would be fairly straight forward but I can't seem to figure out why I am getting this error in the first place because I do have email specified in my create_superuser function. I have looked through several tutorials online and can't see how this is implemented differently. Can someone explain what I am doing wrong?

Ophicleide answered 11/8, 2014 at 8:46 Comment(2)
How do you invoke UserManager.create_superuser()? Do you give it a parameter called email?Emaciation
I am invoking this through ./manage.py createsuperuser and am getting this exception where it should be asking for an email.Ophicleide
P
16

Looking at the code for the management commands, it only prompts for fields in the user model's REQUIRED_FIELDS attribute (as well as username). That attribute contains email by default in AbstractBaseUser, but if you have overridden it - or not inherited from that model in the first place (which you should be doing) - then email will not be prompted, and not passed to the create_superuser method.

Phrensy answered 11/8, 2014 at 9:11 Comment(1)
Thanks added email in the required fields and it workedOphicleide
G
4
class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField('username', max_length=150, unique=True)
    email = models.EmailField('email address', unique=True, max_length = 255)
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

For me above code simply solved problems.

Glissade answered 26/2, 2018 at 0:19 Comment(0)
A
-1

Solution : Check the spelling of your REQUIRED_FIELDS

Analogize answered 13/8, 2021 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.