-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Case insensitive uniqueness validation #4534
Conversation
…ation can be case insensitive
I think I'd rather move |
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.
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): |
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.
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 |
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.
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} |
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.
'%s__%s' % (self.field_name, self.lookup)
@tomchristie have made the requested changes and added a test |
Great stuff! |
Thanks for the quick response on this |
Sometimes fields need to be validated as unique in a case insensitive manner, e.g. a usernames. This PR adds a
ignore_case
argument toUniqueValidator
which causes the queryset lookup to useiexact
.Haven't added any tests yet - wanted to first see if you think this is useful and the right approach.