mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-24 10:34:03 +03:00
Add unaccent
to SearchFilter
This commit is contained in:
parent
f593f5752c
commit
f195dec501
|
@ -230,6 +230,7 @@ The search behavior may be specified by prefixing field names in `search_fields`
|
||||||
| `$` | `iregex` | Regex search. |
|
| `$` | `iregex` | Regex search. |
|
||||||
| `@` | `search` | Full-text search (Currently only supported Django's [PostgreSQL backend][postgres-search]). |
|
| `@` | `search` | Full-text search (Currently only supported Django's [PostgreSQL backend][postgres-search]). |
|
||||||
| None | `icontains` | Contains search (Default). |
|
| None | `icontains` | Contains search (Default). |
|
||||||
|
| `&` | `unaccent` | Accent-insensitive search. (Currently only supported Django's [PostgreSQL backend][postgres-lookups]). |
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
|
@ -370,3 +371,4 @@ The [djangorestframework-word-filter][django-rest-framework-word-search-filter]
|
||||||
[HStoreField]: https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#hstorefield
|
[HStoreField]: https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#hstorefield
|
||||||
[JSONField]: https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#jsonfield
|
[JSONField]: https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#jsonfield
|
||||||
[postgres-search]: https://docs.djangoproject.com/en/stable/ref/contrib/postgres/search/
|
[postgres-search]: https://docs.djangoproject.com/en/stable/ref/contrib/postgres/search/
|
||||||
|
[postgres-lookups]: https://docs.djangoproject.com/en/stable/ref/contrib/postgres/lookups/#unaccent
|
||||||
|
|
|
@ -68,6 +68,7 @@ class SearchFilter(BaseFilterBackend):
|
||||||
'=': 'iexact',
|
'=': 'iexact',
|
||||||
'@': 'search',
|
'@': 'search',
|
||||||
'$': 'iregex',
|
'$': 'iregex',
|
||||||
|
'&': 'unaccent',
|
||||||
}
|
}
|
||||||
search_title = _('Search')
|
search_title = _('Search')
|
||||||
search_description = _('A search term.')
|
search_description = _('A search term.')
|
||||||
|
|
|
@ -10,7 +10,7 @@ from django.test import SimpleTestCase, TestCase
|
||||||
from django.test.utils import override_settings
|
from django.test.utils import override_settings
|
||||||
|
|
||||||
from rest_framework import filters, generics, serializers
|
from rest_framework import filters, generics, serializers
|
||||||
from rest_framework.compat import coreschema
|
from rest_framework.compat import coreschema, postgres_fields
|
||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
from rest_framework.test import APIRequestFactory
|
from rest_framework.test import APIRequestFactory
|
||||||
|
|
||||||
|
@ -305,6 +305,25 @@ class SearchFilterTests(TestCase):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not postgres_fields, reason='psycopg2 is not installed')
|
||||||
|
class SearchPostgreSQLFilterTests(TestCase):
|
||||||
|
|
||||||
|
def test_unaccent_search(self):
|
||||||
|
class SearchListView(generics.ListAPIView):
|
||||||
|
queryset = SearchFilterModel.objects.all()
|
||||||
|
serializer_class = SearchFilterSerializer
|
||||||
|
filter_backends = (filters.SearchFilter,)
|
||||||
|
search_fields = ('title', '&text')
|
||||||
|
|
||||||
|
obj = SearchFilterModel.objects.create(title='Accent títle', text='Accent téxt')
|
||||||
|
view = SearchListView.as_view()
|
||||||
|
request = factory.get('/', {'search': 'accent text'})
|
||||||
|
response = view(request)
|
||||||
|
assert response.data == [
|
||||||
|
{'id': obj.id, 'title': 'Accent títle', 'text': 'Accent téxt'}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class AttributeModel(models.Model):
|
class AttributeModel(models.Model):
|
||||||
label = models.CharField(max_length=32)
|
label = models.CharField(max_length=32)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user