感谢 RA123 的指导,我还覆盖了 MyUser 的保存方法,而不是实现我自己的 UserManager,我实现了默认值并添加了必要的字段:
class MyUser(AbstractBaseUser, PermissionsMixin):
def save(self, *args, **kwargs):
if not self.email_from_work:
self.email_from_work = self.get_email()
super(MyUser, self).save(*args, **kwargs)
def get_email(self):
return self.email
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
username = models.CharField(
_('username'),
max_length=30,
unique=True,
help_text=_('Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[
validators.RegexValidator(
r'^[\w.@+-]+$',
_('Enter a valid username. This value may contain only '
'letters, numbers ' 'and @/./+/-/_ characters.')
),
],
error_messages={
'unique': _("A user with that username already exists."),
},
)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
email = models.EmailField(_('email address'), blank=True)
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=now)
email_from_work = models.EmailField(max_length=255, unique=True)