0
User.objects.filter(name__in=["A", "AB", "a", "ab", "Ab"])

This is an example of using in field lookup in Django.

But I want to know that is here any trick of using in as case insensitive like icontains ?

Thank you

Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39

3 Answers3

3

Others' answers are excellent.

I looked for another way that use Lower function

lower_list = map(lambda x:x.lower(), ["A", "AB", "a", "ab", "Ab"])
User.objects.annotate(name_lower=Lower('name').filter(name_lower__in=lower_list)

It's just another way. I think that other people's answers are close to a good answer.

Kyle
  • 105
  • 3
2

For case insensitive operation you can use iregex

User.objects.filter(name__iregex=r'(A|AB|a|ab|Ab)')

Ref: https://docs.djangoproject.com/en/dev/ref/models/querysets/#iregex

Update: You can make own lookup. Get help from https://docs.djangoproject.com/en/dev/ref/models/lookups/

shafik
  • 6,098
  • 5
  • 32
  • 50
1

According to Django query case-insensitive list match there is no solution other then do iregex lookup

sur0k
  • 334
  • 1
  • 6