簡體   English   中英

無法使用自定義超級用戶登錄到 Django 管理員

[英]Cannot login to Django admin with custom superuser

我正在為我的博客站點開發自定義Author用戶模型。 這個用戶模型中的作者使用外鍵連接到我的另一個應用程序我的帖子應用程序並創建作者-帖子關系。 但是,我什至無法登錄到 Django 管理面板,它說我的密碼錯誤,但我確定它是正確的。 這是什么原因?

請為員工帳戶輸入正確的電子郵件地址和密碼。 兩個地方都要注意大小寫。

我的自定義用戶模型代碼:

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)

修復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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM