当我尝试通过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()
更新从user = self.model中省略is_active = True。新错误是:* TypeError:'is_superuser'是此函数的无效关键字参数*
答案 0 :(得分:4)
我遇到了完全相同的错误,遵循Mike Hibbert关于定义自定义用户的优秀高级教程:http://www.mikesdjangotutorials.co.uk/我能够通过将这些字段添加到我的CustomUser来实现它:
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
...我原本假设它们是从AbstractBaseUser继承而来的,但果然,他们在文档中对此进行了清楚的解释:https://docs.djangoproject.com/en/dev/topics/auth/customizing/#custom-users-and-django-contrib-admin注意如果你想让你的CustomUser工作,那里有一些额外的步骤在您的Django管理员中(从注册等开始)
答案 1 :(得分:3)
您应该删除模型中不使用的每个参数(CustomUser
)init方法和该调用的基类init方法:
user = self.model(email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
您从CustomUser
派生了AbstractBaseUser
,其中没有init并且派生自models.Model。这意味着您可以只传递您在模型上定义的字段的名称:
user = self.model(email=email, your_name=something, company=something)
尝试从CustomUser
AbstractUser