如果您想摆脱该username字段并email用作唯一的用户标识符,则必须创建一个User扩展自定义模型,AbstractBaseUser而不是AbstractUser。确实,username并且email是在中定义的AbstractUser,您不能覆盖它们。这意味着您还必须重新定义要在中定义的所有字段AbstractUser。
from django.contrib.auth.models import ( AbstractBaseUser, PermissionsMixin, BaseUserManager, ) fromdjango.dbimport models fromdjango.utilsimport timezone from django.utils.translation import ugettext_lazy as _ class UserManager(BaseUserManager): use_in_migrations = True def _create_user(self, email, password, **extra_fields): 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_staff', False) extra_fields.setdefault('is_superuser', False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) if extra_fields.get('is_staff') is not True: raise ValueError('Superuser must have 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) class User(AbstractBaseUser, PermissionsMixin): """PermissionsMixin contains the following fields: - `is_superuser` - `groups` - `user_permissions` You can omit this mix-in if you don't want to use permissions or if you want to implement your own permissions logic. """ class Meta: verbose_name = _("user") verbose_name_plural = _("users") db_table = 'auth_user' # `db_table` is only needed if you move from the existing default # User model to a custom one. This enables to keep the existing data. USERNAME_FIELD = 'email' """使用电子邮件作为唯一的用户名。""" REQUIRED_FIELDS = ['first_name', 'last_name'] GENDER_MALE = 'M' GENDER_FEMALE = 'F' GENDER_CHOICES = [ (GENDER_MALE, _("Male")), (GENDER_FEMALE, _("Female")), ] email = models.EmailField( verbose_name=_("email address"), unique=True, error_messages={ 'unique': _( "A user is already registered with this email address"), }, ) gender = models.CharField( max_length=1, blank=True, choices=GENDER_CHOICES, verbose_name=_("gender"), ) first_name = models.CharField( max_length=30, verbose_name=_("first name"), ) last_name = models.CharField( max_length=30, verbose_name=_("last name"), ) is_staff = models.BooleanField( verbose_name=_("staff status"), default=False, help_text=_( "指定用户是否可以登录此管理站点。" ), ) is_active = models.BooleanField( verbose_name=_("active"), default=True, help_text=_( "指定是否应将此用户视为活动用户。 " "取消选择它而不是删除帐户。" ), ) date_joined = models.DateTimeField( verbose_name=_("date joined"), default=timezone.now, ) objects = UserManager()