[英]Cannot login to Django admin with custom superuser
I am developing a custom Author
user model for my blog site.我正在为我的博客站点开发自定义
Author
用户模型。 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.
但是,我什至无法登录到 Django 管理面板,它说我的密码错误,但我确定它是正确的。 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
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:修复
create_user
参数的顺序:
# def create_user(self, firstName, lastName, email, password, **other_fields):
def create_user(self, email, firstName, lastName, password, **other_fields):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.