Django:Create superuser() 得到了一个意外的关键字参数 'password'

Django: Create superuser() got an unexpected keyword argument 'password'

我正尝试按照文档定义我自己的用户模块,但每当我尝试创建我的第一个超级用户时,我都会收到以下错误消息。

TypeError: create_superuser() got an unexpected keyword argument 'password'

有些人似乎通过评论 user.is_staff 和 user.is_admin 解决了这个问题,但它对我不起作用。

有什么建议吗?

我的models.py

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin

# Create your models here.

class MyAccountManager(BaseUserManager):
    def create_user(self, email, first_name, last_name, password=None):
        if not email:
            raise ValueError("Users must have an email address")
        if not first_name or last_name:
            raise ValueError("Users must have a first and last name")

        user = self.model(
            email=self.normalize_email(email),
            first_name=first_name,
            last_name=last_name,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, first_name, last_name):
        user = self.create_user(
            email=self.normalize_email(email),
            password=password,
            first_name=first_name,
            last_name=last_name,
        )

        #user.is_staff = True
        user.is_superuser = True
        #user.is_admin = True
        user.save(using=self._db)
        return user

class Account(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(verbose_name="email", max_length=60, unique=True)
    date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
    last_login = models.DateTimeField(verbose_name='last login', auto_now_add=True)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

    # Stupid name in Django - Basically is the "Login field"
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name']

    objects = MyAccountManager()

    def __str__(self):
        return self.email
    
    def has_perm(self, perm, obj=None):
        return self.is_admin
    
    def has_module_perms(self, app_label):
        return True

在 create_superuser 正文中添加密码作为参数

    def create_superuser(self, email, password, first_name, last_name):
    user = self.model(
        email=self.normalize_email(email),
        password=password,
        first_name=first_name,
        last_name=last_name,
    )

我建议在保存之前使用 Django 方法user.set_password(user.password),因为它会为您散列密码

    def create_superuser(self, password, email, first_name, last_name):
    user = self.model(
        email=self.normalize_email(email),
        password=password,
        first_name=first_name,
        last_name=last_name,
    )
    user.set_password(user.password)
    #user.is_staff = True
    user.is_superuser = True
    #user.is_admin = True
    user.save(using=self._db)
    return user

将密码添加到 create_superuser 的参数:

def create_superuser(self, email, first_name, last_name, password):

请注意,您在 user = self.create_user(**fields) 中使用了 self,因此您的函数也应该有密码 arg。

哪里有方法:

def create_superuser(self, email, first_name, last_name):
        user = self.create_user(
            email=self.normalize_email(email),
            password=password,
            first_name=first_name,
            last_name=last_name,
        )

        #user.is_staff = True
        user.is_superuser = True
        #user.is_admin = True
        user.save(using=self._db)
        return user

不要在 self.create_user 方法中执行 password=password。 相反,请执行以下操作:

user = self.create_user(
    email=self.normalize_email(email),
    first_name=first_name,
    last_name=last_name
)
user.set_password(password)

该方法会处理密码散列并相应地进行设置。