demoslam
demoslam

Reputation: 553

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

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.

Upvotes: 40

Views: 15805

Answers (2)

JamesO
JamesO

Reputation: 25936

icontains is case-insensitive - http://docs.djangoproject.com/en/dev/ref/models/querysets/#std:fieldlookup-icontains

Upvotes: 14

lprsd
lprsd

Reputation: 87077

Use:

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

Upvotes: 75

Related Questions