Django 3.2.2中使用CustomUserManager无法创建超级用户
Django创建超级用户时的TypeError问题解决
首先来看你遇到的错误:
文件 "D:\python\gambling_game\gamegambling\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个
核心问题:多传了self参数
在create_superuser里调用create_user时,你完全不需要手动传递self!因为当你用self.create_user()这种方式调用类的实例方法时,Python会自动把当前的CustomUserManager实例作为第一个参数(也就是create_user方法里的self)传递进去。你额外加了一个self,直接导致参数总数多了一个,这就是触发TypeError的根源。
还有几个小拼写/语法错误需要修正
除了上面的核心问题,代码里还有几个细节错误也会导致运行异常:
self.nomalize_email拼写错误:正确的方法名是self.normalize_email(注意是normalize,少了个r)- 函数名
bith_isValid拼写错误:应该改成birth_isValid(对应birth_date的拼写,把bith改成birth) self.models错误:BaseUserManager里对应的属性是self.model(单数形式,不是复数models)
修正后的完整代码
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 # 创建模型 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')) # 修正拼写错误:nomalize → normalize email = self.normalize_email(email) # 修正函数名拼写:bith_isValid → birth_isValid if birth_isValid(birth_date): birth_date = birth_date else: raise ValueError(_('birth date not valid')) # 修正models → model user = self.model(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') # 移除多余的self参数 return self.create_user(email, pseudo, first_name, last_name, birth_date, password, **other_fields) # 修正函数名拼写:bith_isValid → birth_isValid def birth_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) # 名字(名) first_name = models.CharField(max_length = 255) # 姓氏(姓) 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
把这些修正之后,再尝试创建超级用户应该就能顺利运行了!
内容的提问来源于stack exchange,提问作者urielIsNotMyfavorite
相关产品推荐
相关产品推荐

