Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Last active August 5, 2024 01:50
Show Gist options
  • Save douglasmiranda/a097c5fb18e28fe898dcb913dd6a77c0 to your computer and use it in GitHub Desktop.
Save douglasmiranda/a097c5fb18e28fe898dcb913dd6a77c0 to your computer and use it in GitHub Desktop.
Django - Postgres - Accent-insensitive queries in Portuguese language for full-text search.

Django and Postgres - Accent-insensitive queries in Portuguese.

(it does work for other languages)

well, at this point I just repeat myself over and over again:

Enable UNNACENT in Postgres

CREATE EXTENSION UNACCENT

Create migration

Let Django create an empty migration, so you can customize.

python manage.py makemigrations --empty your_app_name

Then add the operation:

# Generated by Django 5.1b1 on 2024-08-05 01:30

from django.db import migrations


class Migration(migrations.Migration):
    dependencies = [
        ("your_app_name", "django will fill this with the latest migration for the app"),
    ]

    operations = [
        migrations.RunSQL(
            "CREATE TEXT SEARCH CONFIGURATION portuguese_unaccent( COPY = portuguese );"
        ),
        migrations.RunSQL(
            "ALTER TEXT SEARCH CONFIGURATION portuguese_unaccent "
            + "ALTER MAPPING FOR hword, hword_part, word "
            + "WITH unaccent, portuguese_stem;"
        ),
    ]

then just migrate. python manage.py migrate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment