mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 13:54:47 +03:00
Add ignore_case arg to UniqueValidator so that field uniqueness validation can be case insensitive
This commit is contained in:
parent
0b373be712
commit
1bd218ba52
|
@ -42,9 +42,10 @@ class UniqueValidator(object):
|
|||
"""
|
||||
message = _('This field must be unique.')
|
||||
|
||||
def __init__(self, queryset, message=None):
|
||||
def __init__(self, queryset, ignore_case=True, message=None):
|
||||
self.queryset = queryset
|
||||
self.serializer_field = None
|
||||
self.ignore_case = ignore_case
|
||||
self.message = message or self.message
|
||||
|
||||
def set_context(self, serializer_field):
|
||||
|
@ -62,7 +63,8 @@ class UniqueValidator(object):
|
|||
"""
|
||||
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}
|
||||
return qs_filter(queryset, **filter_kwargs)
|
||||
|
||||
def exclude_current_instance(self, queryset):
|
||||
|
|
Loading…
Reference in New Issue
Block a user