Opened 2 years ago

Closed 2 years ago

#29616 closed Cleanup/optimization (fixed)

Do not ask password in createsuperuser command if used custom user model without password field

Reported by: Semyon Pupkov Owned by: Josh Schneier
Component: contrib.auth Version: 2.1
Severity: Normal Keywords: users
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In a case when user model customized as:

from django.contrib.auth.base_user import AbstractBaseUser

class UserManager(BaseUserManager):
    def _create_user(self, portal_user_id, email, **extra_fields):
        del extra_fields['password'] <<-- trick to fix errror

        email = self.normalize_email(email)
        user = self.model(portal_user_id=portal_user_id, email=email, **extra_fields)
        user.save(using=self._db)
        return user

    def create_superuser(self, portal_user_id, email=None, **extra_fields):
        return self._create_user(portal_user_id, email, **extra_fields)


class User(AbstractBaseUser):
    password = None
    last_login = None
    
    objects = UserManager()
    ...

command ./manage.py createsuperuser --email=me@… should not ask password if settings.AUTH_USER_MODEL does not have a password
command ./manage.py createsuperuser --email=me@… --noinput should not pass a password to create_superuser method

Traceback

Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/app/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.venv/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.venv/lib/python3.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 59, in execute
    return super().execute(*args, **options)
  File "/app/.venv/lib/python3.7/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/app/.venv/lib/python3.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 179, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "/app/trc/users/models.py", line 17, in create_superuser
    return self._create_user(portal_user_id, email, **extra_fields)
  File "/app/trc/users/models.py", line 12, in _create_user
    user = self.model(portal_user_id=portal_user_id, email=email, **extra_fields)
  File "/app/.venv/lib/python3.7/site-packages/django/db/models/base.py", line 495, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % kwarg)
TypeError: 'password' is an invalid keyword argument for this function

Change History (4)

comment:1 Changed 2 years ago by Tim Graham

Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization

comment:2 Changed 2 years ago by Josh Schneier

Owner: changed from nobody to Josh Schneier
Status: newassigned

comment:3 Changed 2 years ago by Tim Graham

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:4 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 8b43e9b:

Fixed #29616 -- Fixed createsuperuser for user models that don't have a password field.

Note: See TracTickets for help on using tickets.
Back to Top