Issue with createsuperuser when implementing custom user model

user3282276 picture user3282276 · Aug 11, 2014 · Viewed 9.6k times · Source

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?

Answer

Daniel Roseman picture Daniel Roseman · Aug 11, 2014

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.