def validate_email(self):
"""Ensure emails are unique across the models tracking emails.
Since it's essential to keep email addresses unique to support our
workflows, a `ValidationError` will be raised if the email trying
to be saved is already assigned to some other user.
"""
lookup = Q(email__iexact=self.email)
if self.pk is not None:
# When there's an update, ensure no one else has this address
lookup &= ~Q(user=self)
try:
EmailAddress.objects.get(lookup)
except EmailAddress.DoesNotExist:
pass
else:
raise ValidationError({
'email': [_('This email address already exists.')]
})
评论列表
文章目录