Skip to content

Commit a2bb1fb

Browse files
committed
Add unaccent to SearchFilter
1 parent 9864c47 commit a2bb1fb

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

docs/api-guide/filtering.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ The search behavior may be specified by prefixing field names in `search_fields`
230230
| `$` | `iregex` | Regex search. |
231231
| `@` | `search` | Full-text search (Currently only supported Django's [PostgreSQL backend][postgres-search]). |
232232
| None | `icontains` | Contains search (Default). |
233+
| `&` | `unaccent` | Accent-insensitive search. (Currently only supported Django's [PostgreSQL backend][postgres-lookups]). |
233234

234235
For example:
235236

@@ -370,3 +371,4 @@ The [djangorestframework-word-filter][django-rest-framework-word-search-filter]
370371
[HStoreField]: https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#hstorefield
371372
[JSONField]: https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#jsonfield
372373
[postgres-search]: https://docs.djangoproject.com/en/stable/ref/contrib/postgres/search/
374+
[postgres-lookups]: https://docs.djangoproject.com/en/stable/ref/contrib/postgres/lookups/#unaccent

rest_framework/filters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class SearchFilter(BaseFilterBackend):
6868
'=': 'iexact',
6969
'@': 'search',
7070
'$': 'iregex',
71+
'&': 'unaccent',
7172
}
7273
search_title = _('Search')
7374
search_description = _('A search term.')

tests/test_filters.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django.test.utils import override_settings
1111

1212
from rest_framework import filters, generics, serializers
13-
from rest_framework.compat import coreschema
13+
from rest_framework.compat import coreschema, postgres_fields
1414
from rest_framework.exceptions import ValidationError
1515
from rest_framework.test import APIRequestFactory
1616

@@ -305,6 +305,25 @@ class SearchListView(generics.ListAPIView):
305305
]
306306

307307

308+
@pytest.mark.skipif(not postgres_fields, reason='psycopg2 is not installed')
309+
class SearchPostgreSQLFilterTests(TestCase):
310+
311+
def test_unaccent_search(self):
312+
class SearchListView(generics.ListAPIView):
313+
queryset = SearchFilterModel.objects.all()
314+
serializer_class = SearchFilterSerializer
315+
filter_backends = (filters.SearchFilter,)
316+
search_fields = ('title', '&text')
317+
318+
obj = SearchFilterModel.objects.create(title='Accent títle', text='Accent téxt')
319+
view = SearchListView.as_view()
320+
request = factory.get('/', {'search': 'accent text'})
321+
response = view(request)
322+
assert response.data == [
323+
{'id': obj.id, 'title': 'Accent títle', 'text': 'Accent téxt'}
324+
]
325+
326+
308327
class AttributeModel(models.Model):
309328
label = models.CharField(max_length=32)
310329

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy