Can I easily over-ride Django ORM 'iexact' to use LOWER() instead of UPPER()? Can I easily over-ride Django ORM 'iexact' to use LOWER() instead of UPPER()? postgresql postgresql

Can I easily over-ride Django ORM 'iexact' to use LOWER() instead of UPPER()?


Interesting situation here. I'd never really stopped to think about it before. Seems like the use of UPPER for iexact searches was introduced back in revision 8536, in response to ticket 3575, nearly three years ago. Before that Django had been using ILIKE for these types of searches.

I looked through the backend code and the only thing I can find that points to any reason for UPPER vs LOWER seems to be that Oracle defaults to uppercase in its treatment of case-insensitive data. Since the others are agnostic, it seems Django decided to default to UPPER to cover all the bases.

The other impression I got from looking at the source code was that you're not going to get around using UPPER. It's literally all over the place and not just when actually querying the database. Python's upper string extension is used quite frequently as well.

I'd say your best bet is to simply create an index with upper(column) as well or instead of, and go have a drink.


Try .extra() before going to .raw()

MyModel.objects.extra(where=["lower(mycol)=%s"], params=['foo'])