通过子类化AbstractUser创建class CustomUser
时发生错误,但我没有得到错误时创建class Customuser
通过子类化AbstractBaseUser. 为什么?
class CustomUser(AbstractUser):
# making default username to none instead using email as username field
username = None
email = models.EmailField(_('email address'),unique=True)
name = models.CharField(max_length=255)
"""
Multiple user type without extra fields
# 1. Boolean Select Field
# option to make if user is seller or customer
is_customer = models.BooleanField(default=True)
is_seller = models.BooleanField(default=False)
# 2 Choice Field with djnago multiselect
# type = (
# (1, 'Seller'),
# (2, 'Customer')
# )
# to select one of two choice, to select multi instal package django-multiselectfield
# user_type = models.IntegerField(choices=type, default=1)
# 3 Separate class for different rolles and many to many field in user model
# usertype = models.ManyToManyField(UserType)
"""
# Multiple user with extra field
# 1 boolean field with class for different role
# is_customer = models.BooleanField(default=True)
# is_seller = models.BooleanField(default=False)
# Proxy model
class Types(models.TextChoices):
CUSTOMER = "Customer", "CUSTOMER"
SELLER = "Seller","SELLER"
default_type = Types.CUSTOMER
type = models.CharField(_('Type'),max_length=255, choices=Types.choices, default=default_type)
# if not code below then taking default value in user model not in proxy in model
# The point is that we do not know what arguments save is expecting so this is
# basically saying "any arguments passed into our new save(...) method,
# just hand them off to the old overridden save(...) method,
# positional arguments first followed by any keyword arguments"
def save(self, *args, **kwargs):
if not self.id:
self.type = self.default_type
return super().save(*args, **kwargs)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
# specify all objects for the class comes from
objects = CustomUserManager()
def __str__(self):
return self.name
实际上,问题出现的原因可能是您的CustomUserManager没有从UserManager继承那些预定义的函数。
确保您的CustomUserManager继承了AbstractUser操作所需的所有函数。
,
class CustomUserManager(UserManager):
.....