Django: How to create superuser with custom fields?

Hi there, I have a custom User model in my models.py and custom UserManager as well. My custom User model has username field with changed name to login and same is updated in UserManager as well. Everything goes fine, but when I try to create superuser using command python manage.py createsuperuser, it asks for login but it does not ask for email which at the end gives me this error:

TypeError: UserManager.create_superuser() missing 1 required positional argument: 'email'

My custom User model is:

class User(AbstractBaseUser, PermissionsMixin):
    login = models.CharField(max_length=254, unique=True)
    email = models.EmailField(max_length=254, unique=True)
    first_name = models.CharField(max_length=254, null=True, blank=True)
    last_name = models.CharField(max_length=254, null=True, blank=True)
    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    last_login = models.DateTimeField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    
    USERNAME_FIELD = 'login'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def get_absolute_url(self):
        return "/users/%i/" % (self.pk)

My custom UserManager is:

class UserManager(BaseUserManager):
    def _create_user(self, login, email, password, is_staff, is_admin, **extra_fields):
        if not email:
            raise ValueError('Users must have an email address')

        now = timezone.now()
        email = self.normalize_email(email)

        user = self.model(
            login=login,
            email=email,
            is_staff=is_staff,
            is_active=True,
            is_admin=is_admin,
            last_login=now,
            date_joined=now, 
            **extra_fields
        )

        user.set_password(password)
        user.save(using=self._db)

        return user

    def create_user(self, login, email, password, **extra_fields):
        user = self._create_user(login, email, password, False, False, **extra_fields)
        user.save(using=self._db)
        
        return user


    def create_superuser(self, login, email, password, **extra_fields):
        user=self._create_user(login, email, password, True, True, **extra_fields)

        return user

and in admin.py I have:

class UserAdmin(BaseUserAdmin):
    fieldsets = (
        (None, {'fields': ('login', 'email', 'password', 'first_name', 'last_name', 'last_login')}),
        ('Permissions', {'fields': (
            'is_active', 
            'is_staff', 
            'is_admin',
            'groups', 
            'user_permissions',
        )}),
    )
    add_fieldsets = (
        (
            None,
            {
                'classes': ('wide',),
                'fields': ('login', 'email', 'password1', 'password2')
            }
        ),
    )

    list_display = ('login', 'email', 'first_name', 'last_name', 'is_admin', 'last_login')
    list_filter = ('is_staff', 'is_admin', 'is_active', 'groups')
    search_fields = ('email', 'login',)
    ordering = ('email', 'login',)
    filter_horizontal = ('groups', 'user_permissions',)

Any help will be appreciated.

@KenWhitesell any suggestions??

See the docs for Customizing authentication in Django | Django documentation | Django

1 Like

@KenWhitesell thanks for the guideline.