Skip to content

Case insensitive uniqueness validation #4534

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

Merged
merged 5 commits into from
Oct 4, 2016

Conversation

rowanseymour
Copy link
Contributor

Sometimes fields need to be validated as unique in a case insensitive manner, e.g. a usernames. This PR adds a ignore_case argument to UniqueValidator which causes the queryset lookup to use iexact.

Haven't added any tests yet - wanted to first see if you think this is useful and the right approach.

@xordoquy
Copy link
Collaborator

xordoquy commented Oct 4, 2016

I think I'd rather move filter_kwargs to a get_filter_kwargs() so it's easy to override.
@tomchristie your call on this ?

Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to see this functionality go in, with the alterations suggested.

Also needs documentation.

Great stuff! 😄

@@ -42,10 +42,11 @@ class UniqueValidator(object):
"""
message = _('This field must be unique.')

def __init__(self, queryset, message=None):
def __init__(self, queryset, message=None, ignore_case=True):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably prefer to see us do this by adding a lookup="exact" parameter here that can be modified.
Make it more explicit what's going on under the hood.

self.queryset = queryset
self.serializer_field = None
self.message = message or self.message
self.ignore_case = ignore_case
Copy link
Member

@tomchristie tomchristie Oct 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.lookup = lookup

@@ -62,7 +63,8 @@ def filter_queryset(self, value, queryset):
"""
Filter the queryset to all instances matching the given attribute.
"""
filter_kwargs = {self.field_name: value}
field_lookup = 'iexact' if self.ignore_case else 'exact'
filter_kwargs = {'%s__%s' % (self.field_name, field_lookup): value}
Copy link
Member

@tomchristie tomchristie Oct 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'%s__%s' % (self.field_name, self.lookup)

@rowanseymour
Copy link
Contributor Author

@tomchristie have made the requested changes and added a test

@tomchristie tomchristie modified the milestones: 3.4.8 Release, 3.5.0 Release Oct 4, 2016
@tomchristie tomchristie merged commit 883efbc into encode:master Oct 4, 2016
@tomchristie
Copy link
Member

Great stuff!

@rowanseymour
Copy link
Contributor Author

Thanks for the quick response on this

@rowanseymour rowanseymour deleted the case_insensitive_uniqueness branch October 4, 2016 12:46
@tomchristie tomchristie modified the milestones: 3.4.8 Release, 3.5.0 Release Oct 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants