我想在Django 1.7.1中扩展Django用户类以删除名字和姓氏,并对电子邮件地址设置唯一约束。到目前为止,我有这个自定义模型来更改约束/字段:
class ExtUser(AbstractBaseUser, PermissionsMixin):
class Meta:
# Django 1.7.2?
#default_related_name = 'user'
db_table = 'auth_user'
verbose_name = _('user')
verbose_name_plural = _('users')
abstract = False
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.'), 'invalid')
])
email = models.EmailField(_('email address'), max_length=75, blank=False, null=False, unique=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=timezone.now)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def get_short_name(self):
return self.username
def email_user(self, subject, message, from_email=None, **kwargs):
send_mail(subject, message, from_email, [self.email], **kwargs)
我修改了设置文件:
AUTH_USER_MODEL = 'ext.ExtUser'
我可以登录Django管理员,但是当我尝试编辑用户时,我得到以下异常:
ProgrammingError at /api/extuser/1/
column auth_user_groups.extuser_id does not exist
LINE 1: ...oup"."id" = "auth_user_groups"."group_id" ) WHERE "auth_user...
我猜其他表中的用户外键是根据类名形成的...我该如何解决这个问题?我尝试在模型元类中使用default_related_name字段,但这没有任何积极影响。
提前感谢您的帮助!
亲切的问候, ķ。
答案 0 :(得分:0)
好的,所以我找到了解决问题的方法:确保自定义用户模型类名为" User"而不是" ExtUser"一切都会继续发挥作用。
轻松! : - )