New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use a case-insensitive email field. (Requires Django 1.11.) #169
Conversation
This is cool, but does introduce a requirement on Postgres over other (lesser?) databases. While I'm not sure it's foolproof, I've had success with the following, which I present as an alternative: class User(...):
...
def validate_unique(self, exclude=None):
"""
Checks unique constraints on the model.
Overridden to validate email in a case insensitive manner. Some of the
logic has been derived from _perform_unique_checks().
https://github.com/django/django/blob/1.9.7/django/db/models/base.py#L1014-L1030
"""
# Validate email unless explicitly excluded.
exclude = exclude or []
errors = {}
if 'email' not in exclude:
qs = User.objects.filter(email__iexact=self.email)
if not self._state.adding and self.pk is not None:
qs = qs.exclude(pk=self.pk)
if qs.exists():
errors['email'] = self.unique_error_message(User, ('email',))
exclude.append('email')
try:
super().validate_unique(exclude=exclude)
except ValidationError as error: # pragma: no cover
# email is currently the only unique field, so impossible to test
# this. Skipping from coverage (sorry)!
errors.update(error.error_dict)
if errors:
raise ValidationError(errors) |
@meshy hmm, thanks! @incuna/backend opinions on Charlie's suggestion over my existing implementation? |
Another alternative is to add a |
That's a good idea. |
Actually it kind of requires doubling up on all the mixins. If it's going to be backwards compatible it's probably better to use Charlie's suggestion. Since 1.11 is LTS it's probably ok to drop the support for older versions though. |
If it helps to muddy the waters a little, I actually prefer your version, and would use it if I could |
Lol. I think I prefer it as well because there's less of it (probably works more reliably) but I don't know how much this library is used on non-postgres systems. |
It probably isn't used by anyone but Incuna (and maybe ex-Incuna people). So probably everyone uses postgres. |
my two cents here is a needs a changlog update |
@Minglee01 oh I was going to do that once it's 'complete', but I can do it now |
Any other comments? |
4 similar comments
] | ||
|
||
operations = [ | ||
CITextExtension(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to require superuser to create the extension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, how do we enable that? Would have thought we'd always have a superuser.
@incuna/backend review please?