Django query __startswith is not case sensitive

I have been testing user searching with sorted results and I found this strange behavior

>>> User.objects.filter(username__istartswith="AbC")
<QuerySet [<User: AbC>, <User: AbCuuu>, <User: abc>, <User: abcuuu>]>
>>> User.objects.filter(username__startswith="AbC")
<QuerySet [<User: AbC>, <User: AbCuuu>, <User: abc>, <User: abcuuu>]>

Shouldn’t __startswith only have 2 of those results?
I need to actually search with case sensitivity, how do I do that?

I expect __startswith to be case sensitive and __istartswith to be case insensitive, but both return the same, case insensitive QuerySet

>Solution :

You are most likely using SQLite, where startswith will return the same result as istartswith, due to lack of case sensive LIKE (see text in bold at the bottom):


Case-sensitive starts-with.



SQL equivalent:

SELECT ... WHERE headline LIKE 'Lennon%';

SQLite doesn’t support case-sensitive LIKE statements; startswith acts like istartswith for SQLite.

Leave a Reply