From f195dec501e453520765dbba6064b2de129e40dc Mon Sep 17 00:00:00 2001 From: Marcelo Galigniana Date: Mon, 15 Apr 2024 10:50:12 -0300 Subject: [PATCH 1/2] Add `unaccent` to `SearchFilter` --- docs/api-guide/filtering.md | 2 ++ rest_framework/filters.py | 1 + tests/test_filters.py | 21 ++++++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/filtering.md b/docs/api-guide/filtering.md index ff5f3c7759..de8a5c5579 100644 --- a/docs/api-guide/filtering.md +++ b/docs/api-guide/filtering.md @@ -230,6 +230,7 @@ The search behavior may be specified by prefixing field names in `search_fields` | `$` | `iregex` | Regex search. | | `@` | `search` | Full-text search (Currently only supported Django's [PostgreSQL backend][postgres-search]). | | None | `icontains` | Contains search (Default). | +| `&` | `unaccent` | Accent-insensitive search. (Currently only supported Django's [PostgreSQL backend][postgres-lookups]). | 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 [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-lookups]: https://docs.djangoproject.com/en/stable/ref/contrib/postgres/lookups/#unaccent diff --git a/rest_framework/filters.py b/rest_framework/filters.py index 3f4730da84..d54a3b7338 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -68,6 +68,7 @@ class SearchFilter(BaseFilterBackend): '=': 'iexact', '@': 'search', '$': 'iregex', + '&': 'unaccent', } search_title = _('Search') search_description = _('A search term.') diff --git a/tests/test_filters.py b/tests/test_filters.py index 6db0c3deb2..1e49d08d53 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -10,7 +10,7 @@ from django.test.utils import override_settings 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.test import APIRequestFactory @@ -305,6 +305,25 @@ class SearchListView(generics.ListAPIView): ] +@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): label = models.CharField(max_length=32) From dd7d5fce1cd950fcf2d36f17d09048aff108760b Mon Sep 17 00:00:00 2001 From: Asif Saif Uddin Date: Sun, 27 Apr 2025 07:58:02 +0000 Subject: [PATCH 2/2] Update tests/test_filters.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tests/test_filters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_filters.py b/tests/test_filters.py index 1e49d08d53..0051167d71 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -305,7 +305,7 @@ class SearchListView(generics.ListAPIView): ] -@pytest.mark.skipif(not postgres_fields, reason='psycopg2 is not installed') +@pytest.mark.skipif(not postgres_fields, reason='PostgreSQL support (postgres_fields) is not available') class SearchPostgreSQLFilterTests(TestCase): def test_unaccent_search(self): 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