【发布时间】:2013-11-18 19:25:27
【问题描述】:
当我尝试通过 createsuperuser 管理命令创建用户时,我不断收到此错误:
TypeError: 'is_active' 是此函数的无效关键字参数
我尝试添加 is_active 字段,但无济于事。还尝试弄乱 REQUIRED_FIELDS 因为 django 文档说它们必须匹配 create_superuser 字段。仍然没有运气。
非常感谢您的指导。谢谢!这是我正在使用的自定义用户模型:
class CustomUserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
now = timezone.now()
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
return self._create_user(email, password, False, False, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
return self._create_user(email, password, True, True, **extra_fields)
class Company(models.Model):
company_name = models.CharField(max_length=200)
class CustomUser(AbstractBaseUser):
your_name = models.CharField(max_length=254, blank=True)
company = models.ForeignKey(Company)
email = models.EmailField(blank=True, unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['your_name',]
objects = CustomUserManager()
更新省略 is_active = True from user = self.model。新错误是:*TypeError: 'is_superuser' is an invalid keyword argument for this function*
【问题讨论】:
-
你是如何调用创建超级用户的?
-
第一次运行syncdb时,当它询问你是否想创建你的第一个超级用户时:“你刚刚安装了Django的auth系统,这意味着你没有定义任何超级用户。你会现在想创建一个吗?”
-
如果省略
is_active,会发生什么?(对于AbstractBaseUser,无论如何默认为True)。 -
从 user = self.model 中移除 is_active = true。现在我收到错误 TypeError: 'is_superuser' is an invalid keyword argument for this function
-
别忘了
PermissionsMixin