TypeError: User() got an unexpected keyword argument 'is_staff' when using CustomUser
TypeError: User() got an unexpected keyword argument 'is_staff' when using CustomUser
我正在使用 AbstractBaseUser 创建自定义用户,但每当我尝试创建超级用户时,它都会显示
TypeError: User() got an unexpected keyword argument 'is_staff'
user=self._create_user(email, password, True, True, **extra_fields)
File "C:\Users\user1\django\customuserproject\users\models.py", line 24, in _create_user
如何解决这个问题?我已经查看了有关此问题的其他 Whosebug 帖子,但它们对我没有帮助。我还没有尝试过普通用户(我需要一个超级用户来创建一个普通用户),但我相信它会是一样的
我的models.py:
class UserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
if not email:
raise ValueError('Users must have an email address')
now = timezone.now()
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, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
return self._create_user(email, password, False, False, **extra_fields)
def create_staffuser(self, email, password, **extra_fields):
"""
Creates and saves a staff user with the given email and password.
"""
user=self._create_user(email, password, True, False, **extra_fields)
user.save(using=self._db)
return user
def create_superuser(self, email, password, **extra_fields):
"""
Creates and saves a superuser with the given email and password.
"""
user=self._create_user(email, password, True, True, **extra_fields)
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
first_name = models.CharField(max_length=70, null=True, blank=True)
last_name = models.CharField(max_length=70, null=True, blank=True)
display_name = models.CharField(('display name'), max_length=70, blank=True, null=True, unique=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False) # a admin user; non super-user
is_superuser = models.BooleanField(default=False) # a superuser
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
# notice the absence of a "Password field", that is built in.
objects = UserManager()
USERNAME_FIELD = 'email'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name'] # Email & Password are required by default.
class Meta:
verbose_name = ('user')
verbose_name_plural = ('users')
#db_table = 'auth_user'
abstract = False
def get_absolute_url(self):
return "/users/%i/" % (self.pk)
@property
def name(self):
if self.first_name:
return self.first_name
elif self.display_name:
return self.display_name
return 'You'
def get_full_name(self):
"""
Return the first_name plus the last_name, with a space in between.
"""
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
def __str__(self):
return self.email
def natural_key(self):
return (self.email,)
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_staff
@property
def is_superuser(self):
return self.is_superuser
@property
def is_active(self):
return self.is_active
尝试将 getter
和 setters
与 @property
一起使用,它们的效果要好得多,而且实施得很清楚。
class Demo:
def __init__(self):
self._age = 0
# using property decorator
# a getter function
@property
def age(self):
print("getter method called")
return self._age
# a setter function
@age.setter
def age(self, a):
if(a < 18):
raise ValueError("Sorry you age is below eligibility criteria")
print("setter method called")
self._age = a
感谢@Iain Shelvington、@Willem Van Onsem 和@7u5h4r。
我通过删除我声明的所有属性解决了这个问题,同时只留下变量:is_staff,is_superuser, is_active.
我正在使用 AbstractBaseUser 创建自定义用户,但每当我尝试创建超级用户时,它都会显示
TypeError: User() got an unexpected keyword argument 'is_staff'
user=self._create_user(email, password, True, True, **extra_fields)
File "C:\Users\user1\django\customuserproject\users\models.py", line 24, in _create_user
如何解决这个问题?我已经查看了有关此问题的其他 Whosebug 帖子,但它们对我没有帮助。我还没有尝试过普通用户(我需要一个超级用户来创建一个普通用户),但我相信它会是一样的
我的models.py:
class UserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
if not email:
raise ValueError('Users must have an email address')
now = timezone.now()
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, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
return self._create_user(email, password, False, False, **extra_fields)
def create_staffuser(self, email, password, **extra_fields):
"""
Creates and saves a staff user with the given email and password.
"""
user=self._create_user(email, password, True, False, **extra_fields)
user.save(using=self._db)
return user
def create_superuser(self, email, password, **extra_fields):
"""
Creates and saves a superuser with the given email and password.
"""
user=self._create_user(email, password, True, True, **extra_fields)
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
first_name = models.CharField(max_length=70, null=True, blank=True)
last_name = models.CharField(max_length=70, null=True, blank=True)
display_name = models.CharField(('display name'), max_length=70, blank=True, null=True, unique=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False) # a admin user; non super-user
is_superuser = models.BooleanField(default=False) # a superuser
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
# notice the absence of a "Password field", that is built in.
objects = UserManager()
USERNAME_FIELD = 'email'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name'] # Email & Password are required by default.
class Meta:
verbose_name = ('user')
verbose_name_plural = ('users')
#db_table = 'auth_user'
abstract = False
def get_absolute_url(self):
return "/users/%i/" % (self.pk)
@property
def name(self):
if self.first_name:
return self.first_name
elif self.display_name:
return self.display_name
return 'You'
def get_full_name(self):
"""
Return the first_name plus the last_name, with a space in between.
"""
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
def __str__(self):
return self.email
def natural_key(self):
return (self.email,)
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_staff
@property
def is_superuser(self):
return self.is_superuser
@property
def is_active(self):
return self.is_active
尝试将 getter
和 setters
与 @property
一起使用,它们的效果要好得多,而且实施得很清楚。
class Demo:
def __init__(self):
self._age = 0
# using property decorator
# a getter function
@property
def age(self):
print("getter method called")
return self._age
# a setter function
@age.setter
def age(self, a):
if(a < 18):
raise ValueError("Sorry you age is below eligibility criteria")
print("setter method called")
self._age = a
感谢@Iain Shelvington、@Willem Van Onsem 和@7u5h4r。
我通过删除我声明的所有属性解决了这个问题,同时只留下变量:is_staff,is_superuser, is_active.