From deb6657c0d620be1d3e34f0b17deffee711c313e Mon Sep 17 00:00:00 2001 From: Marcelo Galigniana Date: Mon, 15 Apr 2024 10:50:12 -0300 Subject: [PATCH 1/3] Add `unaccent` to `SearchFilter` --- docs/api-guide/filtering.md | 2 ++ rest_framework/filters.py | 1 + 2 files changed, 3 insertions(+) diff --git a/docs/api-guide/filtering.md b/docs/api-guide/filtering.md index 8573c25915..f09b222570 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/stable/ref/contrib/postgres/fields/#hstorefield [JSONField]: https://docs.djangoproject.com/en/stable/ref/models/fields/#django.db.models.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 10d861c87d..acfa3bb7e3 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -58,6 +58,7 @@ class SearchFilter(BaseFilterBackend): '=': 'iexact', '@': 'search', '$': 'iregex', + '&': 'unaccent', } search_title = _('Search') search_description = _('A search term.') From fb3b63ad8b6ae60c880eb2f0a18bee1809da40f8 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 6 Apr 2026 18:37:16 -0300 Subject: [PATCH 2/3] Setup CI to run tests with Postgres --- .github/workflows/main.yml | 21 +++++++++++++++++++++ pyproject.toml | 4 ++++ tests/conftest.py | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5b74c0c217..7e1784516d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,6 +28,21 @@ jobs: name: Python ${{ matrix.python-version }} runs-on: ubuntu-24.04 + services: + postgres: + image: postgres:16 + env: + POSTGRES_DB: postgres + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + strategy: matrix: python-version: @@ -37,6 +52,9 @@ jobs: - '3.13' - '3.14' + env: + DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres + steps: - uses: actions/checkout@v6 @@ -52,6 +70,9 @@ jobs: - name: Install dependencies run: python -m pip install --upgrade tox + - name: Create unaccent extension + run: PGPASSWORD=postgres psql -h localhost -U postgres -d postgres -c 'CREATE EXTENSION IF NOT EXISTS unaccent;' + - name: Run tox targets for ${{ matrix.python-version }} run: tox run -f py$(echo ${{ matrix.python-version }} | tr -d . | cut -f 1 -d '-') diff --git a/pyproject.toml b/pyproject.toml index a8304e715f..bf5ce6ffa1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,7 @@ dev = [ { include-group = "test" }, ] test = [ + "dj-database-url>=3.1.0", "importlib-metadata<10.0", # Pytest for running the tests. "pytest==9.*", @@ -116,6 +117,9 @@ keep_full_version = true [tool.pytest.ini_options] addopts = "--tb=short --strict-markers -ra" testpaths = [ "tests" ] +markers = [ + "requires_postgres: marks tests as requiring a PostgreSQL database backend", +] filterwarnings = [ "ignore:'cgi' is deprecated:DeprecationWarning", ] diff --git a/tests/conftest.py b/tests/conftest.py index 11a079f484..db1b79254e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,8 @@ import os +import dj_database_url import django +import pytest from django.core import management @@ -13,10 +15,13 @@ def pytest_addoption(parser): def pytest_configure(config): from django.conf import settings - settings.configure( - DEBUG_PROPAGATE_EXCEPTIONS=True, - DEFAULT_AUTO_FIELD="django.db.models.AutoField", - DATABASES={ + if os.getenv('DATABASE_URL'): + databases = { + 'default': dj_database_url.config(), + 'secondary': dj_database_url.config(), + } + else: + databases = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:' @@ -24,8 +29,13 @@ def pytest_configure(config): 'secondary': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:' - } - }, + }, + } + + settings.configure( + DEBUG_PROPAGATE_EXCEPTIONS=True, + DEFAULT_AUTO_FIELD="django.db.models.AutoField", + DATABASES=databases, SITE_ID=1, SECRET_KEY='not very secret in tests', USE_I18N=True, @@ -65,6 +75,12 @@ def pytest_configure(config): ), ) + # Add django.contrib.postgres when using a PostgreSQL database + if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql': + settings.INSTALLED_APPS += ( + 'django.contrib.postgres', + ) + # guardian is optional try: import guardian # NOQA @@ -91,3 +107,13 @@ def pytest_configure(config): if config.getoption('--staticfiles'): management.call_command('collectstatic', verbosity=0, interactive=False) + + +def pytest_collection_modifyitems(config, items): + from django.conf import settings + + if settings.DATABASES['default']['ENGINE'] != 'django.db.backends.postgresql': + skip_postgres = pytest.mark.skip(reason='Requires PostgreSQL database backend') + for item in items: + if 'requires_postgres' in item.keywords: + item.add_marker(skip_postgres) From 69967f954e7368df6c5fddef197497313c87a394 Mon Sep 17 00:00:00 2001 From: Marcelo Galigniana Date: Mon, 6 Apr 2026 23:33:12 -0300 Subject: [PATCH 3/3] Add test for `unaccent` filter --- tests/test_filters.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_filters.py b/tests/test_filters.py index d438eda0f3..df7da7380e 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -247,6 +247,26 @@ def as_sql(self, compiler, connection): {'id': 2, 'title': 'zz', 'text': 'bcd'}, ] + @pytest.mark.requires_postgres + def test_search_field_with_unaccent(self): + SearchFilterModel.objects.create(title='Jeremy', text='jeremy') + SearchFilterModel.objects.create(title='Jérémy', text='jérémy') + SearchFilterModel.objects.create(title='Jérémie', text='jérémie') + SearchFilterModel.objects.create(title='Jeremie', text='jeremie') + + class SearchListView(generics.ListAPIView): + queryset = SearchFilterModel.objects.all() + serializer_class = SearchFilterSerializer + filter_backends = (filters.SearchFilter,) + search_fields = ('&title',) + + view = SearchListView.as_view() + + request = factory.get('/', {'search': 'Jerem'}) + response = view(request) + assert len(response.data) == 4 + assert {item['title'] for item in response.data} == {'Jeremy', 'Jérémy', 'Jérémie', 'Jeremie'} + def test_search_field_with_multiple_words(self): class SearchListView(generics.ListAPIView): queryset = SearchFilterModel.objects.all()