如何解决TypeError:create_superuser()在Django中出现了意外的关键字参数“ paid”

时间:2019-10-03 03:47:58

标签: python django python-3.x django-2.2

我正在实现扩展AbstractBaseUser的自定义用户模型。 这是我的account / models.py文件。

from django.db import models
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)


class MyUserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

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

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email, password.
        """
        user = self.create_user(
            email,
            password=password
        )

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

class MyUser(AbstractBaseUser):
    email = models.EmailField(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=True)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    paid = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False) 
    is_staff = models.BooleanField(default=False)

    list_filter = ()  # Con
    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ("paid",)
    objects = MyUserManager()

    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 self.is_admin

但是在使用命令python manage.py runserver创建超级用户时出现错误

这是我从航站楼得到的:

Email: i@j.com
Paid: True
Password: 
Password (again): 
Traceback (most recent call last):
    File "manage.py", line 21, in <module>
        main()
    File "manage.py", line 17, in main
        execute_from_command_line(sys.argv)
    File "/home/rhidwan/Desktop/shotave/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
        utility.execute()
    File "/home/rhidwan/Desktop/shotave/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/rhidwan/Desktop/shotave/venv/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
    File "/home/rhidwan/Desktop/shotave/venv/lib/python3.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 61, in execute
    return super().execute(*args, **options)
    File "/home/rhidwan/Desktop/shotave/venv/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
      output = self.handle(*args, **options)
    File "/home/rhidwan/Desktop/shotave/venv/lib/python3.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 156, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() got an unexpected keyword argument 'paid'

以前大约4天前我最后一次检查时,此方法工作正常。但是它从今天开始停止工作。不知道是什么问题。我没有对软件包进行任何更新。而且不记得我是否删除了任何代码。

预先感谢

修改 这是我解决问题的方法。 我删除了所有迁移,然后将“付费”字段更改为is_paid。然后再次应用迁移,它就像魅力一样。

感谢所有为解决问题提供帮助的人。

2 个答案:

答案 0 :(得分:1)

您的create_superuser不接受任何关键字参数paid。它只接受emailpassword。但是您已将其添加到REQUIRED_FIELDS,因此在创建超级用户时它将被传递到create_superuser

您必须更新create_superusercreate_user方法以接受该字段。

一个简单的解决方案是更改:

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

收件人:

def create_superuser(self, email, password, **extra_fields):
    ...
    user = self.create_user(
        email,
        password=password,
        **extra_fields,
    )

然后更改:

def create_user(self, email, password=None):
    ...
    user = self.model(
        email=self.normalize_email(email)
    )

收件人:

def create_user(self, email, password, **extra_fields):
    ...
    user = self.model(
        email=self.normalize_email(email),
        **extra_fields,
    )

此后,将来,您也可以将其他字段也添加到REQUIRED_FIELDS,而无需更改代码。

答案 1 :(得分:1)

在用户模型中使用REQUIRED_FIELDS时,python manage.py createsuperuser命令将提示您输入必填字段,然后将其发送到管理员的create_superuser模块。

由于您已经在create_superuser()方法中初始化了付费字段,因此您可以执行以下操作之一:

  1. 如果要在命令提示符下设置paid参数,请从paid=True函数中删除create_superuser()并进行如下更改:
    def create_superuser(self, *args, **kwargs):
        """
        Creates and saves a superuser with the given email, password.
        """
        user = self.create_user(
            **kwargs
        )
        user.is_superuser = True
        user.is_admin = True
        user.is_staff = True
        user.save(using=self._db)
        return user
  1. 或者,如果要特定于参数,则可以在函数参数中传递付费参数:
def create_superuser(self, email, password, paid):
        """
        Creates and saves a superuser with the given email, password.
        """
        user = self.create_user(
            email,
            password=password
        )

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

  1. 如果paid=True始终是SUPERUSER的True条件,则可以将其从REQUIRED_FIELDS中删除,但对于普通用户也将删除它,但是默认值将设置为False根据您的模型。