How can I activate the unaccent extension on an already existing model How can I activate the unaccent extension on an already existing model postgresql postgresql

How can I activate the unaccent extension on an already existing model


A migration file needs to be manually made and applied.

First, create an empty migration:

./manage.py makemigrations myapp --empty

Then open the file and add UnaccentExtension to operations:

from django.contrib.postgres.operations import UnaccentExtensionclass Migration(migrations.Migration):    dependencies = [        (<snip>)    ]    operations = [        UnaccentExtension()    ]

Now apply the migration using ./manage.py migrate.

If you'd get following error during that last step:

django.db.utils.ProgrammingError: permission denied to create extension "unaccent"HINT:  Must be superuser to create this extension.

... then temporarily allow superuser rights to your user by performing postgres# ALTER ROLE <user_name> SUPERUSER; and its NOSUPERUSER counterpart. pgAdminIII can do this, too.

Now enjoy the unaccent functionality using Django:

>>> Person.objects.filter(first_name__unaccent=u"Helène")[<Person: Michels Hélène>]