Django - Can't login into admin site with user create directly from admit site

So, i have created a Customer user model using AbstractUser

Model.py

class Department(models.Model):
    department_name = models.CharField(max_length=20)

    def __str__(self):
        return self.department_name


class Role(models.Model):
    role_name = models.CharField(max_length=120)

    def __str__(self):
        return self.role_name

class Task(models.Model):
    task_desp = models.TextField(max_length=100)
    in_progress = models.BooleanField()
    
    def __str__(self):
        return self.task_desp

class Employee(AbstractUser):
    username = None
    email = models.EmailField(max_length=100,unique=True)
    phone_number = models.CharField(max_length=14)
    department = models.ForeignKey(Department,on_delete=models.CASCADE,null=True,blank=True)
    role = models.ForeignKey(Role,on_delete=models.CASCADE,null=True,blank=True)
    address = models.TextField(max_length=200)
    task_assigned = models.ForeignKey(Task,on_delete=models.CASCADE,null=True,blank=True)

    USERNAME_FIELD= 'email'
    REQUIRED_FIELDS = ['phone_number']

    objects= UserManager()

    def __str__(self):
        return self.email

manager.py

from django.contrib.auth.base_user import BaseUserManager 

class UserManager(BaseUserManager):

    use_in_migrations = True

    def create_user(self,email,password=None,**extra_fields):
        if not email:
            raise ValueError("Please provide email")

        email = self.normalize_email(email)
        user = self.model(email=email,**extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user    

    def create_superuser(self,email,password,**extra_fields):
        extra_fields.setdefault('is_staff',True)
        extra_fields.setdefault('is_superuser',True)
        extra_fields.setdefault('is_active',True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError("super user must have staff user")

        return self.create_user(email,password,**extra_fields)    

Added the model in setting

AUTH_USER_MODEL = 'emp.Employee'

So,I have made 3 user via python manage.py createsuperuser and was able to login into admit site, but when I made a 4th user directly from admin site, gave it all staff and superuser permission I am not able to login

I get this error

Please enter the correct email and password for a staff account. Note that both fields may be case-sensitive.

enter image description here

I create this Elon user ( ignore the name ) directly from the admin panel but I am not able to log in with the email and password provided despite I have checked staff and superuser options. I think the password which is being shown direct maybe the issue because for the other 3 users the password is encrypted.

On save -adminmodel saves password directly, not the hash. You should do yourself on the model save in model admin:

Def model_save(sale, obj, *args, **kwargs):
    If not obj.pk:
        obj.set_password(password)
    super().model_save()
Back to Top