Good Day,
I am trying to create a custom user model with AbstractBaseUser.
models.py
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext as _
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin
# Create your models here.
class CustomAccountManager(BaseUserManager):
def create_user(self, email, username, password, **other):
email = self.normalize_email(email)
user = self.model(
email=email,
username=username,
**other
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
user = self.create_user(
email,
password=password,
username=username,
)
user.is_admin = True
user.save(using=self._db)
return user
class NewUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email adress'), unique=True)
username = models.CharField(max_length=100, unique=True)
start_date = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = CustomAccountManager()
def __str__(self):
return self.email
I used the documentation to create it.
https://docs.djangoproject.com/en/4.0/topics/auth/customizing/
I deleted my database, made makemigrations, migrate and created a superuser like in the screenshot:

With the password ‘admin’ and email ‘admin@admin.com’.
But it doesnt work as you see in the screenshot.
Do you know the issue?
Thank you very much! 🙂
Edit:
settings.py
INSTALLED_APPS = [
'abstractuser',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
AUTH_USER_MODEL = 'abstractuser.NewUser'
>Solution :
The is_staff field is what determines whether the user may login to the admin site. In your code, you’re not setting this property to True when creating superusers.
You need to either set user.is_staff = True in your create_superuser function or replace the is_staff field with a property that reads from is_admin.
def create_superuser(self, email, username, password):
user = self.create_user(
email,
password=password,
username=username,
)
user.is_admin = True
user.is_staff = True # can access the admin site
user.save(using=self._db)
return user