Reputation: 103
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 '[email protected]'. 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'
Upvotes: 0
Views: 766
Reputation: 1
Make sure to add the following methods to your Custom User Model. These are the methods required by django-admin panel. Source - django
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
Upvotes: 0
Reputation: 40891
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
Upvotes: 1