无法在django 1.8中创建超级用户使用自定义用户模型

时间:2015-06-04 08:30:12

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

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

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
你可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

REQUIRED_FIELD = ['username'] 

应改为:

REQUIRED_FIELDS = ['username']