【问题标题】:Cannot create super user use custom user model in django 1.8无法在 django 1.8 中创建超级用户使用自定义用户模型
【发布时间】:2015-06-04 08:30:11
【问题描述】:

我尝试制作自定义用户模型,我想制作超级用户,但我遇到了这样的错误

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 50, in execute
    return super(Command, self).execute(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 149, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() takes exactly 4 arguments (3 given)

这是我的model.py

class MyUserManager(BaseUserManager):
    def create_user(self, username, email, password=None):
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email       = self.normalize_email(email),
            username    = username,
        )

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

    def create_superuser(self, username, email, password):
        user                = self.create_user(email,username=username, password=password)
        user.is_admin       = True
        user.save(using=self._db)
        return user

class Operator(AbstractBaseUser, PermissionsMixin):

    owner           = models.ForeignKey('Owner')
    kontraktor      = models.ForeignKey('Kontraktor')
    alphanumeric    = RegexValidator(r'^[0-9a-zA-Z]*$', message='hanya yang mengandung karakter alphanumeric')
    email           = models.EmailField(verbose_name='email address', unique=True, max_length=244)
    username        = models.CharField(unique=True, max_length=20, validators=[alphanumeric])
    first_name      = models.CharField(max_length=30, null=True, blank=True)
    last_name       = models.CharField(max_length=30, null=True, blank=True)
    date_of_birth   = models.DateTimeField(auto_now_add=True)
    date_joined     = models.DateTimeField(auto_now_add=True)
    is_active       = models.BooleanField(default=True, null=False)
    is_staff        = models.BooleanField(default=False, null=False)

    objects         = MyUserManager()

    USERNAME_FIELD  = 'email'
    REQUIRED_FIELD  = ['username']

    def get_full_name(self):
        fullname = self.first_name+" "+self.last_name
        return self.fullname

    def get_short_name(self):
        return self.username

    def __str__(self):
        return self.email

你能帮我解决这个问题吗?

【问题讨论】:

  • 什么时候收到这个错误?你还没有在上面的代码中调用它。
  • 这不是您提到的问题,但请注意,当您调用 create_user 时,您以错误的顺序传递值,这样会失败。
  • 当我尝试在终端@rnevius 中创建超级用户时
  • 那么,该怎么做呢? @丹尼尔罗斯曼
  • 您是否提供密码?此外,正如@DanielRoseman 所提到的,在您从create_superuser 调用的create_user 方法中修复值的顺序,以匹配事物的定义方式(首先是username,然后是email,然后是password) .

标签: python django django-models django-admin django-custom-user


【解决方案1】:
REQUIRED_FIELD = ['username'] 

应该改为:

REQUIRED_FIELDS = ['username']

【讨论】:

    猜你喜欢
    • 2013-04-05
    • 2013-12-23
    • 1970-01-01
    • 2013-09-29
    • 2020-08-22
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多