Superadmin login is asking for staff account
I am trying to create a custom user model in my Django project. Here are my code:
models.py:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
############# Handles the creation of regular users and
# superusers (admin users) with custom logic. ############
class MyAccountManager(BaseUserManager):
# Creates a regular user with the necessary fields
def create_user(self, first_name, last_name, username, email, password=None):
if not email:
raise ValueError('User must have an email address')
if not username:
raise ValueError('User must have a username')
user = self.model(
email = self.normalize_email(email), # This line normalizes the email address
#that means it converts the domain part of the email address to lowercase.
username = username,
first_name = first_name,
last_name = last_name,
)
user.set_password(password)
user.save(using=self._db)
return user
# Creates a superuser with the necessary fields
def create_superuser(self, first_name, last_name, username, email, password=None):
user = self.create_user( # This line calls the create_user method to create a regular user.
email = self.normalize_email(email),
username = username,
first_name = first_name,
last_name = last_name,
)
user.is_admin = True
user.is_staff = True
user.is_active = True
user.is_superadmin = True
user.save(using=self._db)
return user
######################## This is a custom user model that represents a user
# with fields that go beyond the default Django user model.#################
class Account (AbstractBaseUser):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
username = models.CharField(max_length=50, unique=True)
email = models.EmailField(max_length=100, unique=True)
phone_number = models.CharField(max_length=50)
# Required fields for the AbstractBaseUser class.
date_joined = models.DateTimeField(auto_now_add=True) # This field is automatically set when the object is first created.
last_login = models.DateTimeField(auto_now=True) # This field is automatically set when the object is updated.
is_admin = models.BooleanField(default=False) # This field is used to determine if the user has administrative privileges.
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_superadmin = models.BooleanField(default=False)
USERNAME_FIELD = 'email' # This line makes sure that user will login using email instead of username.
REQUIRED_FIELDS = ['username', 'first_name', 'last_name'] # These fields are required when creating a user.
objects = MyAccountManager() # In Django, the objects attribute is the default manager for model queries
# to get all accounts). By setting objects = MyAccountManager(), you replace the default manager
# with a custom one, MyAccountManager. In summary, this line ensures that the Account model uses the MyAccountManager for database operations, enabling custom user creation logic.
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, add_label):
return True
admin.py:
from django.contrib import admin
from .models import Account
# Register your models here.
admin.site.register(Account)
settings.py:
AUTH_USER_MODEL = 'accounts.Account'
I have successfully created a super user. But when I am trying to login 'Django Administration', it is showing the message "Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive."
I have used "pip install django==3.1" when downloading Django.
I have provided correct email and password. Also I have attempted several times by deleting db.sqlite3 and migrating from scratch but could not find any way.