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):
startswith
Case-sensitive starts-with.
Example:
Entry.objects.filter(headline__startswith='Lennon')
SQL equivalent:
SELECT ... WHERE headline LIKE 'Lennon%';
SQLite doesn’t support case-sensitive
LIKE
statements;startswith
acts likeistartswith
for SQLite.
https://docs.djangoproject.com/en/4.1/ref/models/querysets/#startswith