[英]Django custom user model with unique_together on the email
I'm trying to create a custom User model in my Django app, the problem is I get an error saying email must be unique (fair enough!), however, I need email
and company
together to be unique, as I may have the same email but registered to a different company. 我正在尝试在Django应用中创建自定义用户模型,问题是我收到一条错误消息,指出电子邮件必须是唯一的(足够公平!),但是,我需要
email
和company
一起才能唯一,因为我可能拥有相同的电子邮件,但注册到其他公司。
I get the following error: 我收到以下错误:
ERRORS:
site.SiteUser: (auth.E003) 'SiteUser.email' must be unique because it is named as the 'USERNAME_FIELD'.
Here is my model: 这是我的模型:
class SiteUser(models.Model):
company = models.ForeignKey(Company)
email = models.EmailField(max_length=254)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(auto_now=False, auto_now_add=True)
objects = SiteUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Meta:
unique_together = ('company', 'email',)
Add auth.E003
to the SILENCED_SYSTEM_CHECKS
setting. 将
auth.E003
添加到SILENCED_SYSTEM_CHECKS
设置。 This will allow manage.py
to run. 这将允许
manage.py
运行。 And I think you should add W004
warning to this list too: 而且我认为您也应该在此列表中添加
W004
警告:
SILENCED_SYSTEM_CHECKS = ['auth.E003', 'auth.W004']
You're missing the unique=True
in your email field definition. 您在电子邮件字段定义中缺少
unique=True
。 The filed that is used in the USERNAME_FIELD
should have this argument as explained in the django doc on USERNAME_FIELD . USERNAME_FIELD
使用的文件应具有此参数,如USERNAME_FIELD上django doc中所述。
It should be like this: 应该是这样的:
email = models.EmailField(max_length=254, unique=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.