我正在根据以下教程创建一个 Django 自定义用户:
class UserProfileManager(BaseUserManager):
"""Helps Django work with our custom user model."""
def create_user(self, email, name, password=None):
"""Creates a user profile object."""
if not email:
raise ValueError('Users must have an email address.')
email = self.normalize_email(email)
user = self.model(email=email, username=name)
user.user_id = -1
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, name, password):
"""Creates and saves a new superuser with given details."""
user = self.create_user(email = email, name = name, password = password)
user.is_superuser = True
user.save(using=self._db)
class CustomUser(AbstractBaseUser, PermissionsMixin):
"""Represents a user profile inside our system"""
email = models.EmailField(max_length=255, unique=True)
username = models.CharField(max_length=255, unique=True)
user_id = models.IntegerField()
objects = UserProfileManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def __str__(self):
return self.email
当我尝试创建超级用户时出现以下错误:
TypeError:create_superuser() 得到了一个意外的关键字参数“用户名”
但是,如果我将“用户名”字段的名称更改为“名称”,我将能够创建一个没有错误的超级用户!有谁知道为什么我不能将字段命名为“名称”以外的任何名称?