# login as a superuser using a custom user model doesn't work

**URL:** <https://forum.djangoproject.com/t/login-as-a-superuser-using-a-custom-user-model-doesnt-work/25059>\
**Category:** Mystery Errors\
**Created:** [November 5, 2023, 5:31pm UTC](https://forum.djangoproject.com/t/login-as-a-superuser-using-a-custom-user-model-doesnt-work/25059 "2023-11-05T17:31:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackFormeDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jackformedev/32/16852_2.png) [@JackFormeDev](https://forum.djangoproject.com/u/JackFormeDev)\
**Post date:** [November 5, 2023, 5:31pm UTC](https://forum.djangoproject.com/t/login-as-a-superuser-using-a-custom-user-model-doesnt-work/25059/1 "2023-11-05T17:31:31Z")

</div>

Hey Guys.  
I am creating a website, **my problem is in the superuser**.  
I have created a custom user model.  
here the code:

```auto
class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self, email, password, **extra_fields):
        """
        Create and save a user with the given email and password.
        """
        if not email:
            raise ValueError(_("The Email must be set"))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        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(_("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, password, **extra_fields)

class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_("email address"), unique=True)
    username_account = models.CharField(max_length=20)
    description = models.TextField(blank=True)
    photo = models.ImageField(upload_to='images/', blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)
    date_joined = models.DateTimeField(default=timezone.now)

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__ (self):
        return self.email
    
    def save(self, *args, **kwargs):
        self.password = make_password(self.password)
        super(CustomUser, self).save(*args, **kwargs)

```

until recently everything was working perfectly.  
now, the creation of a superuser happens successfully. however if i try to login in the admin page it always gives me this error:  
_‘Please enter the correct email address and password for a staff account. Note that both fields may be case-sensitive.’_  
I made sure the credentials were correct, also, the superuser has:  
is\_staff = True  
is\_active = True  
is\_superuser = True  
(I checked the internet and that was the problem for the most part).  
I honestly don’t know what to do, I tried creating a custom authentication but the problem persists.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 5, 2023, 8:20pm UTC](https://forum.djangoproject.com/t/login-as-a-superuser-using-a-custom-user-model-doesnt-work/25059/2 "2023-11-05T20:20:27Z")

</div>

> [@JackFormeDev](#):
>
> ```auto
> def save(self, *args, **kwargs):
> self.password = make_password(self.password)
> 
> ```

You don’t want to do this. If the password has already been hashed and you try to save the object again, it’s going to consider the `self.password` field to be the plaintext password and hash it again.

If you read the source code for `AbstractBaseUser`, you’ll see that is defines a `set_password` method. Also, if you read the code for the `_create_user` method in `UserManager` you’ll see that _it_ calls `make_password` to set the password on the new user object.

---

<div class="post-metadata">

**Author:** ![JackFormeDev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jackformedev/32/16852_2.png) [@JackFormeDev](https://forum.djangoproject.com/u/JackFormeDev)\
**Post date:** [November 6, 2023, 1:46pm UTC](https://forum.djangoproject.com/t/login-as-a-superuser-using-a-custom-user-model-doesnt-work/25059/3 "2023-11-06T13:46:30Z")

</div>

It worked ! I thank you infinitely
