gpt4 book ai didi

python - 无法登录我的 super 用户(自定义用户模型/经理)

转载 作者:行者123 更新时间:2023-12-01 07:17:01 25 4
gpt4 key购买 nike

我正在尝试使用自定义用户模型和自定义用户管理器来构建 super 用户。我做了与 django 文档关于 create_superuser 方法完全相同的事情,并且在我的 shell 中,我能够使用电子邮件和密码创建一个 super 用户。但是当我尝试登录 django 管理页面时,我遇到了这个奇怪的错误:

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

from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.db import models
from multiselectfield import MultiSelectField

class UserManager(BaseUserManager):
#custom create_user method
def create_user(self, email, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email = self.normalize_email(email)
)
user.set_password = password
user.save(using=self._db)
return user

#Custom create_super_user method
def create_superuser(self, email, password=None):
user = self.create_user(
email = self.normalize_email(email),
password = password
)
user.admin = True
user.save(using=self._db)
return user


class User(AbstractBaseUser):
#setting up Choices for interest, Must add other fields ...
MATHS = 'mat'
PHYSICS = 'phy'
HISTORY = 'his'
BIOLOGIE = 'bio'
ECONOMICS = 'eco'
POLITICS = 'pol'
MUSIC = 'mus'
ENGLISH = 'eng'
FRENCH = 'fra'
SPANISH = 'spa'
LAW = 'law'
COMPUTER_SCIENCE = 'cs'
COMMUNICATION = 'com'
MARKETING = 'mar'
SPORT = 'spo'
INTERESTS_CHOICES = (
(MATHS, 'Maths'),
(PHYSICS, 'Physics'),
(HISTORY, 'History'),
(BIOLOGIE, 'Biologie'),
(ECONOMICS, 'Economics'),
(POLITICS, 'Politics'),
(MUSIC, 'Music'),
(ENGLISH, 'English'),
(FRENCH, 'French'),
(SPANISH, 'Spanish'),
(LAW, 'Law'),
(COMPUTER_SCIENCE, 'Computer Science'),
(COMMUNICATION, 'Communication'),
(MARKETING, 'Marketing'),
(SPORT, 'Sport')
)
interests = MultiSelectField(
max_length = 2,
choices = INTERESTS_CHOICES
)


#Setting up a Ranking System
RANKING_CHOICES = [
('silver', 'Silver'),
('gold', 'Gold'),
('platinium', 'Platinium'),
('diamond', 'Diamond')
]

email = models.EmailField(
max_length=50,
unique=True
)
username = models.CharField(
max_length=25,
unique=True,
null=True,
blank=True
)
date_joined = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=70)
birth_date = models.DateField(null=True, blank=True)
reputation = models.PositiveIntegerField(default=0)
active = models.BooleanField(default=True)
rank = models.CharField(choices=RANKING_CHOICES, max_length=1)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
bio = models.TextField(
max_length=300,
default="",
blank=True
)
objects = UserManager()
#Setting email to be the main source of authentication
USERNAME_FIELD = 'email'

#Super User Only
REQUIRED_FIELDS = ['password']

#def get_absolute_url(self):
#use reverse + nom de l'url de view

def __str__(self):
return self.email

def get_full_name(self):
return f"{self.first_name} {self.last_name}"

def get_short_name(self):
return self.first_name

def get_username(self):
return self.username

def set_user_league(self):
if 15 <= self.reputation < 40:
self.rank = "gold"
elif 40 <= self.reputation < 80:
self.rank = "platinium"
else:
self.rank = "diamond"

最佳答案

您通过调用 .set_password(..) 设置密码,而不是分配新值:

class UserManager(BaseUserManager):

#custom create_user method
def create_user(self, email, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email = self.normalize_email(email)
)
<b>user.set_password(password)</b>
user.save(using=self._db)
return user

默认的create_superuser也会将is_staffis_superuser设置为True:

#Custom create_super_user method
def create_superuser(self, email, password=None):
user = self.create_user(
email = self.normalize_email(email),
password = password
)
<b>user.admin = user.is_superuser = user.is_staff = True</b>
user.save(using=self._db)
return user

关于python - 无法登录我的 super 用户(自定义用户模型/经理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57903995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com