[Answered ]-Can't create superuser with custom userprofile

2👍

You should define your own Manager to instead of the default UserManager;

As your example:

class MyManager(UserManager):
    def create_superuser(self, membernumber,email,password,**extra_fields):
        super().create_superuser(membernumber,email,password,**extra_fileds)
        #or do something to save your model by yourself;

Why we need do this?

Just look your error dump

self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)

So when you run python manage.py createsuperuser on your shell,after you input your membernumber,password,email value,then your user_data will become:

{'membernumber':'Garreth','password':'123456','email':'like@gamil.com'}

but UserManager.create_superuser need username param,your miss it!! So you get that error.

Here is the the source of UserManager.create_superuser(refer 1)

def create_superuser(self, username, email, password, **extra_fields):
    extra_fields.setdefault('is_staff', True)
    extra_fields.setdefault('is_superuser', True)

    if extra_fields.get('is_staff') is not True:
        raise ValueError('Superuser must have is_staff=True.')
    if extra_fields.get('is_superuser') is not True:
        raise ValueError('Superuser must have is_superuser=True.')

    return self._create_user(username, email, password, **extra_fields)

And some source about what run when you python manage.py createsuperuser(refer 2)

if username:
    user_data[self.UserModel.USERNAME_FIELD] = username
    user_data['password'] = password

    self.UserModel._default_manager.db_manager(database)
        \.create_superuser(**user_data)
    if options['verbosity'] >= 1:
        self.stdout.write("Superuser created successfully.")

Suggestion:

If you still want to exec python manager.py createsuperuser,you should NEVER define a custom UserModel that inherit AbstractBaseUser,because django.contrib.admin,django.contrib.auth use too many AbstractUser Model attrs like is_active,is_superuser,so you will get many errors when you visit yousite.com/admin on browser.

Refers:

1.https://github.com/django/django/blob/master/django/contrib/auth/models.py#L156

2.https://github.com/django/django/blob/master/django/contrib/auth/management/commands/createsuperuser.py#L170

👤gkiwi

0👍

In this book said:

Option 2: Subclass AbstractUser:

Choose this option if you like Django’s User model fields the way they are, but need extra fields.

Maybe you have to subclass AbstractBaseUser. See here or a full example.

Edit

USERNAME_FIELD should be used if you want to use another username field. If you want that, you must subclass AbstractBaseUser instead of AbstractUser, because the last one already has defined a USERNAME_FIELD

Hope helps!

0👍

This is clearly a bug, it needs to be fixed. Alternatively, you can create the user with python manage.py shell

from django.contrib.auth import get_user_model get_user_model().objects.create_superuser(username="admin", password="password")

Leave a comment