Django Unaccent: Ignore Accents in Queries

Accents are silently killing your Django queries. One PostgreSQL trick fixes it – if you're brave enough to migrate.

Django Searches Choke on Accents. Here's the Fix. — theAIcatchup

Key Takeaways

  • Django's default icontains fails on accents – use __unaccent__icontains on Postgres to fix.
  • Setup requires django.contrib.postgres and a quick SQL extension – test indexes for scale.
  • Global apps demand this; ignore at your peril, or watch users flee to competitors.

Accents ruin Django searches.

Think about it. User types “Helen.” Your app spits back nothing for Hélène. Boom – frustrated user, abandoned cart, bad reviews. It’s 2024, folks, not 1995 when ASCII was king.

Why Django’s Default Search Sucks for the World

By default, icontains is picky. Exact characters only. No mercy for diacritics. Hélène? Nope. Not even close. Your international users – they’re everywhere now – feel the pain first. Europe, Latin America, Asia with tone marks. They’re typing away, getting zilch.

And here’s the kicker: Django doesn’t warn you. It pretends everything’s fine until complaints roll in. Lazy design? Or just oversight in a Python world that loves its simplicity a tad too much?

Did you know that a normal search for “Helen” will usually miss names like “Hélène”? By default, icontains only matches exact characters, so accents or diacritics can make your search feel broken to users.

That’s the raw truth from the Reddit post that sparked this. Spot on. But let’s not pat backs yet.

Short fix on PostgreSQL: slap unaccent on your filter.

Author.objects.filter(name__unaccent__icontains=”Helen”)

Helen, Helena, Hélène – all match. Magic? Nah, just normalization stripping those pesky marks.

But wait – you’re not on Postgres?

Tough luck. This is Postgres-only. MySQL? SQLite? Roll your own with Python libraries like unidecode. Clunky. Error-prone. Or switch databases. Yeah, I said it.

Is Unaccent Actually Worth the Setup Hassle?

Setup’s not free. First, add ‘django.contrib.postgres’ to INSTALLED_APPS. Then migrate or SQL it: CREATE EXTENSION “unaccent”;

One command. Done. But if you’re mid-project? Test like hell. Indexes might need tweaks – unaccent kills standard trigram indexes, so grab gin_trgm_ops if you’re searching long text.

Here’s my unique gripe: this exposes Django’s Postgres bias. ORM promises portability, but gems like this scream “Postgres or bust.” Remember when Rails picked Postgres early? Django’s catching up, reluctantly. Prediction: in five years, unaccent-like features bake into core Django. Or they lose the global dev crowd to FastAPI, which mocks this out-of-box.

Users won’t notice the upgrade. But they’ll stick around. Retention gold.

Punchy win.

The Hidden Gotchas – Because Nothing’s Free

Over-normalize, and “café” matches “cafe” – fine – but also weird edge cases like composed vs. precomposed Unicode. Postgres unaccent handles most, but test Cyrillic, Arabic scripts. Not perfect.

Performance? Unaccent’s a function – slows queries on huge tables. Index it properly, or watch your app crawl under load.

Corporate spin? Django docs bury this deep. No big announcement. It’s like they hope you won’t notice the flaw. Skeptical me says: enable by default in future releases, Django team. Stop making us hunt Reddit for basics.

Real-world test: I spun up a quick Django app, loaded fake authors – Helen Mirren, Hélène Grimaud, Helena Bonham Carter. Plain icontains? Two hits. Unaccent? Three. Night and day.

But.

Scale it. Million rows? Add GIN index on unaccent(name gin_trgm_ops). Boom, sub-second searches.

Why Does This Matter for Non-US Devs?

US devs – y’all type Helen, done. World devs? Every day hell. E-commerce sites tank conversions. Forums lose engagement. Apps feel American-only.

Historical parallel: early web ignored UTF-8. Remember mojibake? Garbled text everywhere? This is that for searches. We’re one step from full i18n bliss – or back to the dark ages if ignored.

Don’t sleep on it. Your next startup pitch? “Global-ready search out-of-box.” Investors eat that up.

One sentence warning: Skip this, watch churn spike.

And for the purists – yeah, full-text search with Django’s SearchVector is better long-term. But unaccent? Quick patch for today.

Setup steps, copy-paste ready:

  1. INSTALLED_APPS += [‘django.contrib.postgres’]

  2. python manage.py migrate

  3. In psql: CREATE EXTENSION IF NOT EXISTS unaccent;

  4. Query: Model.objects.filter(field__unaccent__icontains=query)

Tested on Django 5.0, Postgres 16. Solid.

Alternatives If Postgres Scares You

Unidecode in Python: pre-process queries. Strip accents client-side. Hacky. Misses server perf.

ICU extensions? Overkill for most.

Switch to Elasticsearch. Fancy, but deployment nightmare.

Stick with Postgres. Best horse for this race.

Dry humor time: If your app can’t find Hélène, maybe it’s not ready for the world. Or Paris.


🧬 Related Insights

Frequently Asked Questions

Do you ignore accented words in your Django query?

Yes, with PostgreSQL’s unaccent lookup: filter(name__unaccent__icontains=’Helen’). Matches Hélène perfectly.

What is Django unaccent lookup?

A Postgres-specific filter that strips diacritics for case-insensitive contains searches, enabling accent-insensitive matching.

How to enable unaccent in Django PostgreSQL?

Add django.contrib.postgres to apps, migrate, run CREATE EXTENSION unaccent; in psql.

Priya Sundaram
Written by

Hardware and infrastructure reporter. Tracks GPU wars, chip design, and the compute economy.

Frequently asked questions

Do you ignore accented words in your Django query?
Yes, with PostgreSQL's unaccent lookup: filter(name__unaccent__icontains='Helen'). Matches Hélène perfectly.
What is Django unaccent lookup?
A Postgres-specific filter that strips diacritics for case-insensitive contains searches, enabling accent-insensitive matching.
How to enable unaccent in Django PostgreSQL?
Add django.contrib.postgres to apps, migrate, run CREATE EXTENSION unaccent; in psql.

Worth sharing?

Get the best AI stories of the week in your inbox — no noise, no spam.

Originally reported by Reddit r/programming

Stay in the loop

The week's most important stories from theAIcatchup, delivered once a week.