Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive. While in command prompt the user is created successfully and is in the database

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'

poa11.04

what could be the problem. Kindly help

You should try to get the object of this user within shell and see if is_staff and is_superuser is True or not.

Side Note: If you are extending PermissionsMixin to your custom user than you do not need is_superuser to be defined explicitly, PermissionsMixin already have this in it.

I have tried to get the object of the user within the shell and both the .is_staff and .is_superuser return True. i have also tried to remove the permissionsMixin but the issue is still the same.

Instead of removing permissionsMixin you should have removed is_superuser from User Model

Yes i did that that but the problem persists

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=150, unique = True, blank=True)
    username = 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_superadmin = models.BooleanField(default=False)
    
    objects = UserManager()
    
   
    
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [
        'username', 
        'first_name', 
        'last_name', 
        'phone_number'
    ]
    

Try to use this account manager it might be because of the manager you are using gives the error

class AccountManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, email, name, phone, password, **extra_fields):

        values = [email, name, phone]
        field_value_map = dict(zip(self.model.REQUIRED_FIELDS, values))

        for field_name, value in field_value_map.items():
            if not value:
                raise ValueError('The {} value must be set'.format(field_name))

        email = self.normalize_email(email)

        user = self.model(
            email=email,
            name=name,
            phone=phone,
            **extra_fields
        )

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

        return user

    def create_user(self, email, name, phone, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, name, phone, password, **extra_fields)

    def create_superuser(self, email, name, phone, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        
        return self._create_user(email, name, phone, password, **extra_fields)

Note: if you are using this manager than run migrations command again and create a superuser again.

I have used the manager with a new database and run migrations a fresh. it has run properly without any errors. i have created a superuser successfully and confirmed that the user object has is_staff and is_superuser returns True in the python Shell. But still cant login with the details. could it be a django issue i am using version 4.2.2 and python 3.11.2. This is my new implementation.

class AccountManager(BaseUserManager):
    use_in_migrations = True


    def _create_user(self, email,username, first_name, last_name, phone_number, password,**extra_fields):

        values = [email, username, first_name, last_name, phone_number]
        field_value_map = dict(zip(self.model.REQUIRED_FIELDS, values))

        for field_name, value in field_value_map.items():
            if not value:
                raise ValueError('The {} value must be set'.format(field_name))

        email = self.normalize_email(email)

        user = self.model(
            email=email, 
            username=username, 
            first_name=first_name, 
            last_name=last_name, 
            phone_number=phone_number, 
            **extra_fields
        )

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

        return user

    def create_user(self, email,username, first_name, last_name, phone_number, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, username, first_name, last_name, phone_number, password, **extra_fields)

    def create_superuser(self, email, username, first_name, last_name, phone_number, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        
        return self._create_user(email, username, first_name, last_name, phone_number, password, **extra_fields)



class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=150, unique = True, blank=True)
    username = 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_superadmin = models.BooleanField(default=False)
    
    objects = AccountManager()    
   
    
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [
        'username', 
        'first_name', 
        'last_name', 
        'phone_number'
    ]
    
    
    def __str__(self):
        return self.email

poa11.04

Are you trying to login with username? if so then try to login with email and password because in your User Model you have specified USERNAME_FIELD = 'email'.

no i am logging with email the admin site asks for email

Create new superadmin and when asks for details copy admin@admin.com and paste it in email and password and try to login with it.
I’m just saying this so that there might not be any typos just copy and paste it and then try :face_with_monocle:.

still the same issue

How are you creating superuser like with python manage.py createsuperuser command or by entering data directly to DB.
Also are you doing any custom validation check before logging in to Admin site.

Also, In the settings file if SESSION_ENGINE value set to this 'django.contrib.sessions.backends.cache' than change the SESSION_ENGINE to 'django.contrib.sessions.backends.db' and try to login than.

I use the command python manage.py createsuperuser and no any custom validation checks am performing

the SESSION_ENGINE value was not even set in my settings i have tried both values django.contrib.sessions.backends.cache and django.contrib.sessions.backends.db

I

The manager you provided here worked have realized that we had not set the .is_active so by default it was false. Thanks

Well in the accounts manager I never sets is_active to True, I set is_active by default True within User Model. Is that the problem, now is it working?

Yes it is working properly