Using Unaccent in Model database function

266 views
Skip to first unread message

Alex Scott

unread,
Jan 5, 2020, 2:12:45 PM1/5/20
to Django users
Hi all,

I just started using the Unaccent extension for Postgres and implementing queryset filters with it like:

q = 'hello'
queryset.filter(name__unaccent__istartswith=q)

I'm now trying to annotate the queryset result with the search index:

queryset.annotate(search_index=StrIndex(Lower('name'), Value(q)))

That works fine by itself for unaccented text, but I'm trying to figure out a way to apply Unaccent to the name variable there, something like:

queryset.annotate(search_index=StrIndex(Lower('name__unaccent'), Value(q)))

--> This has no effect, does the same as the above.

or

queryset.annotate(search_index=StrIndex(Lower(Unaccent('name')), Value(q)))

--> There doesn't seem to be an Unaccent function

Is there a way to do this currently?

Thanks,
Alex

Alex Scott

unread,
Jan 5, 2020, 2:50:37 PM1/5/20
to Django users
I was able to figure this out using...

from django.contrib.postgres.lookups import Unaccent

queryset.annotate(search_index=StrIndex(Unaccent(Lower('name')), Value(q)))

Might be nice to add to the documentation here:
Reply all
Reply to author
Forward
0 new messages