How can django sql queries use case insensitive and contains at the same time?

Question:

Suppose I have two users with username ‘AbA’ and ‘aBa’ in the database.
My query word is ‘ab’.

I used

User.objects.filter(username__contains='ab')

and

User.objects.filter(username__iexact='ab')

These two queries get empty result. However, I want to use something like username__contains__iexact='ab' that can retrieve both ‘AbA’ and ‘aBa’.

Anyone know how to resolve this problem? Thanks.

Asked By: demoslam

||

Answers:

Use:

User.objects.filter(username__icontains='ab')
Answered By: lprsd
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.