无法在 django 3.2.2 中使用自定义用户管理器创建超级用户



我正在尝试创建一个超级用户,但无论我做了什么更改,我仍然会收到以下错误:

文件"D: \python\gampling_game\gamebagging\users\models.py";,第40行,在create_superuser 中

return self.create_user(self、email、pseudo、first_name、last_name、birth_date、password、**other_fields(

TypeError:create_user((接受7个位置参数,但有8个给定了

我对代码进行了多次检查,但没有发现错误。

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser , PermissionsMixin , BaseUserManager
from django.utils.translation import gettext_lazy as _
from datetime import datetime
# Create your models here.
class CustomUserManager(BaseUserManager):
def create_user(self, email, pseudo, first_name, last_name, birth_date, password, **other_fields):
if not email:
raise ValueError(_('Please enter an email address'))
email = self.nomalize_email(email)
if bith_isValid(birth_date):
birth_date = birth_date
else:
raise ValueError(_('birth date not valid'))

user = self.models(email = email, pseudo = pseudo , first_name = first_name,
last_name = last_name, birth_date = birth_date, **other_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, pseudo, first_name, last_name, birth_date, 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('Superuser must be assigned to is_staff=True')
if other_fields.get('is_superuser') is not True:
raise ValueError('Superuser must be assigned to is_superuser=True')
if other_fields.get('is_active') is not True:
raise ValueError('Superuser must be assigned to is_active=True')

return self.create_user(self, email, pseudo, first_name, last_name, birth_date, password, **other_fields)



def bith_isValid(date):
present = datetime.date.today()
return date < present
class CustomUser (AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length = 255, unique = True)
pseudo = models.CharField(max_length = 255, unique = True)
#prenom
first_name = models.CharField(max_length = 255)
#nom
last_name = models.CharField(max_length = 255)
USER_GENDER = [ ('M', 'Male'),
('F', 'Female'),
('NB', 'Non-Binary')]
gender = models.CharField(max_length = 2, choices = USER_GENDER, null = True)
birth_date = models.DateField(auto_now=False, auto_now_add=False)
balance = models.IntegerField(default = 0)
profil_picture = models.ImageField(upload_to='profile_image', blank = True)
is_staff = models.BooleanField(default = False)
is_active = models.BooleanField(default = False)
last_login = models.DateTimeField(auto_now=True)
join_date = models.DateTimeField(default = timezone.now)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['pseudo','first_name','last_name','birth_date']
def __str__(self):
return self.pseudo

不要将self传递给函数调用self
更改此项:

return self.create_user(self, email, ps...)

到此:

return self.create_user(email, ps

PS:可能是复制粘贴错误;(我们都这么做。

最新更新