2 回答
TA贡献1735条经验 获得超5个赞
此代码适用于上述情况
class UserManager(BaseUserManager):
def create_user(self, mobile_no, role, password=None):
"""
Creates and saves a User with the given email and password.
"""
if not mobile_no:
raise ValueError('Users must have an Mobile No')
user = self.model(
mobile_no=mobile_no,
)
user.set_password(password)
user.activated=True
user.save(using=self._db)
return user
def create_superuser(self, mobile_no, role, password):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(mobile_no, role=role,
password=password
)
user.is_admin = True
user.activated=True
user.save(using=self._db)
return user
def create_staffuser(self, mobile_no, role, password):
"""
Creates and saves a staffuser with the given email and password.
"""
user = self.create_user(mobile_no, role=role,
password=password
)
user.is_staff = True
user.activated=True
user.save(using=self._db)
return user
class Users(AbstractBaseUser, PermissionsMixin):
object = UserManager()
mobile_no = models.IntegerField(_('MobNumber'), null=True, blank=True,unique=True)
email = models.EmailField(_('Email'), max_length=75, null=False, blank=False)
first_name = models.CharField(_('FirstName'), max_length=50, null=True, blank=True)
last_name = models.CharField(_('LastName'), max_length=70, null=True, blank=True)
role = models.CharField(_('Role'), max_length=70, null=True, blank=True)
location = models.CharField(_('Location'), max_length=70, null=True, blank=True)
date_time = models.DateTimeField(_('DateTime'), auto_now=True, null=True, blank=True)
activated = models.BooleanField(_('Activated'), default=False)
is_admin = models.BooleanField(_('is_admin'), default=False)
is_staff = models.BooleanField(_('is_staff'), default=False)
def __unicode__(self):
return str(self.mobile_no)
def __str__(self):
return str(self.mobile_no)
def get_full_name(self):
return self.first_name + " " + self.last_name
class Meta:
ordering = ['-id']
@property
def is_staff(self):
return self.is_admin
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return self.is_admin
USERNAME_FIELD = 'mobile_no'
REQUIRED_FIELDS = ['role']
TA贡献1884条经验 获得超4个赞
您可能遇到此问题,因为在必填字段中,您只指定了电子邮件和角色,而没有指定名字、姓氏和位置。在声明用户用户管理器时,您没有为这些字段提供默认值,因此您必须在必填字段中指定它们。那应该可以解决您的问题。
添加回答
举报