Django – Insensitive Case

When using Django in Python there’s these awesome things called QuerySets that are a full blown ORM to make it so I don’t have to think in SQL to get things in and out of a database! One of the things I didn’t find documented very well was case sensitivity vs insensitivity.

If you’re using the MySQL backend the case-sensitive is implied, i.e. using BINARY LIKE instead of just LIKE

series = Series.objects.filter(name__contains=srch_str)

To make sure that you’re searching for some that’s case insensitive you can do the following:

series = Series.objects.filter(name__icontains=srch_str)

If you want it to work case insensitive no matter the backend, collation, or collection type just use icontain, istartswith, iendswith, iregex, iexact.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *