Home > other >  Cannot login to Django admin with custom superuser
Cannot login to Django admin with custom superuser

Time:10-25

I am developing a custom Author user model for my blog site. The authors in this user model connect to my post application, another application of mine, with a foreign key and create the author-post relationship. However, I can't even log in to the Django admin panel, it says my password is wrong, but I'm sure it's correct. What is the reason of this?

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

My custom user model code:

from django.contrib.auth.base_user import BaseUserManager
from django.db import models
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import (
    AbstractBaseUser,
    PermissionsMixin,
)


class CustomAccountManager(BaseUserManager):

    def create_superuser(self, email, firstName, lastName, password, **other_fields):

        other_fields.setdefault('is_staff', True)
        other_fields.setdefault('is_superuser', True)
        other_fields.setdefault('is_active', True)

        if other_fields.get("is_staff") is not True:
            raise ValueError("Buraya erişim izniniz bulunmamaktadır. Lütfen yöneticiyle iletişime geçiniz.")

        if other_fields.get("is_superuser") is not True:
            raise ValueError("Buraya erişim sadece En üst düzey kullanıcılar içindir. Lütfen yöneticiyle iletişime Geçiniz")

        return self.create_user(email, firstName, lastName, password, **other_fields)

    def create_user(self, firstName, lastName, email, password, **other_fields):

        if not email:
            raise ValueError(_("Email Adresinizi Doğrulamalısınız!"))

        email = self.normalize_email(email)
        user = self.model(email=email, firstName=firstName, lastName=lastName, **other_fields)

        user.set_password(password)
        user.save()

        return user


class Author(AbstractBaseUser, PermissionsMixin):

    id = models.AutoField(primary_key=True)
    firstName = models.CharField(max_length=100)
    email = models.EmailField(_("email adresi"), max_length=100, unique=True)
    lastName = models.CharField(max_length=100)
    displayName = models.CharField(max_length=300)
    gender = models.CharField(max_length=50)
    avatar = models.ImageField(upload_to="avatar")
    bgImage = models.ImageField(upload_to="background")
    slug = models.SlugField(editable=False, unique=True)
    desc = models.TextField()
    jobName = models.CharField(max_length=50, default="Author Job")
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    objects = CustomAccountManager()

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["firstName", "lastName"]

    def __str__(self):
        return self.displayName

    def get_slug(self):
        slug = slugify(self.title.replace("ı", "i"))
        unique = slug
        number = 1

        while Author.objects.filter(slug=unique).exists():
            unique = "{}-{}" .format(slug, number)
            number  = 1

        return unique

CodePudding user response:

def create_superuser(self, email, firstName, lastName, password, **other_fields):

    # ...

    return self.create_user(email, firstName, lastName, password, **other_fields)

Fix the order of create_user parameters:

# def create_user(self, firstName, lastName, email, password, **other_fields):
def create_user(self, email, firstName, lastName, password, **other_fields):
  • Related