The problem is that i have created a new superuser from the command prompt which is successful. but when i try to login to the admin section with the same credentials it brings up the error ‘Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive.’ the user is being saved in the database. i have tried a couple of solutions from issues raised on the same but with no success. Here is my models.py.
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
class UserManager(BaseUserManager):
def create_user(
self,
email,
user_name,
first_name,
last_name,
phone_number,
password
):
if not email:
raise ValueError('User must have an email address')
if not user_name:
raise ValueError('User must have a username')
email = self.normalize_email(email),
user = self.model(
email=email,
user_name=user_name,
first_name=first_name,
last_name=last_name,
phone_number=phone_number
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(
self,
email,
user_name,
first_name,
last_name,
phone_number,
password
):
user = self.create_user(
email = email,
user_name = user_name,
first_name = first_name,
last_name = last_name,
phone_number = phone_number,
password = password
)
user.is_active = True
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=150, unique = True, blank=True)
user_name = models.CharField(max_length=50, unique = True)
first_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
phone_number = models.CharField(max_length=30, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(auto_now_add=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=True)
is_active = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = [
'user_name',
'first_name',
'last_name',
'phone_number'
]
def __str__(self):
return self.email
Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
]
AUTH_USER_MODEL = 'users.User'
what could be the problem. Kindly help