我创建了一个包含客户用户模型的应用,该应用继承自AbstractBaseUser
并创建了UserManager
。一切正常,除非我尝试创建第二个超级用户,它取代了第一个超级用户。此外,当我登录管理页面时,它不允许我编辑具有超级用户状态的用户。我不确定导致它的原因,但以下是我使用的代码...
models.py
class User(AbstractBaseUser, PermissionsMixin):
id = models.CharField(_('customer id'), primary_key=True, max_length=255)
email = models.EmailField(_('email address'), unique=True)
first_name = models.CharField(_('first name'), max_length=30)
last_name = models.CharField(_('last name'), max_length=30)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin site.'))
is_active = models.BooleanField(_('active'), default=True,
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
phoneNumber = PhoneNumberField(unique=True)
#restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE,null=True, blank=True)
orderStatus = models.BooleanField('Order Placed', default=False)
numOffense = models.PositiveIntegerField('# Offense', default=0)
company = models.CharField(_('company'), null=True, blank=True, max_length=50)
handle = models.SlugField(unique=True, max_length=30)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name','last_name','phoneNumber']
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def get_full_name(self):
'''
Returns 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):
'''
Returns the short name for the user.
'''
return self.first_name
managers.py
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_superuser', False)
extra_fields.setdefault('is_staff',False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_staff', True)
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
如果您能找到任何需要更改的内容,请与我们联系。谢谢!