Using PostgreSQL unaccent in Django 1.7
Solution taken from this fix [1] to this issue [2].
It looks like this is going to be supported in Django 1.8.
Add this code to your models.py:
from django.db.models import Transform
class Unaccent(Transform):
bilateral = True
lookup_name = 'unaccent'
def as_postgresql(self, compiler, connection):
lhs, params = compiler.compile(self.lhs)
return "UNACCENT(%s)" % lhs, params
models.CharField.register_lookup(Unaccent)
models.TextField.register_lookup(Unaccent)
And create the unaccent extension on your database:
CREATE EXTENSION unaccent;
Then you can use it in your filters, like this:
users = User.objects.filter(
first_name__unaccent__icontains='João'
)
[1] https://github.com/django/django/commit/17fe0bd808a47f37dd1351adb01a8ad2cc852f24
[2] https://code.djangoproject.com/ticket/23423
Written by Daniel Hartmann
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#