类型错误:create_user() 缺少 2 个必需的位置参数:Django Rest Framework 中的“电子邮件”和“密码”

我正在尝试仅使用电子邮件和密码创建超级用户,但出现上述错误。

运行 py manage.py createsuperuser 后,它只要求输入 admin 和密码,我提供了相应的字段,但是在使用 yes 绕过密码验证后,它给出了上述错误。

class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self,first_name,last_name,email,password,**extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError("The email must be set")
        first_name = first_name.capitalize()
        last_name = last_name.capitalize()
        email = self.normalize_email(email)

        user = self.model(
            first_name=first_name,last_name=last_name,email=email,**extra_fields
        )
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self,**extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff',True)
        extra_fields.setdefault('is_superuser',True)
        extra_fields.setdefault('is_active',True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(email,**extra_fields)


class CustomUser(AbstractUser):
    username = None
    #email = models.EmailField(_('email address'),unique=True)
    email = models.EmailField(unique=True)

    username_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email
yuhailin405 回答:类型错误:create_user() 缺少 2 个必需的位置参数:Django Rest Framework 中的“电子邮件”和“密码”

您将 emailpassword 作为位置参数传递给您创建的 create_user 方法,但它需要 4 个位置参数。您提供的参数被传递到 first_namelast_name 而不是 email 和密码。只需将某些内容传递到这些字段中或将它们设置为不需要。

本文链接:https://www.f2er.com/632086.html

大家都在问