I have a model:
class Skill(models.Model):
name = models.CharField(max_length=120)
icon = models.CharField(max_length=20)
color = models.CharField(max_length=7)
I want to do the following filter, but case insensitive:
skill_names = ['Code Quality', 'Analytical Thinking', 'Productivity', 'Communication']
Skill.objects.filter(name__in=skill_names)
CodePudding user response:
I hope this would help you.
from django.db.models import Q
skill_names = ['Code Quality', 'Analytical Thinking', 'Productivity', 'Communication']
query = Q()
for skill in skill_names:
query |= Q(name__contains=skill)
Skill.objects.filter(query)
CodePudding user response:
I would probably do something like this (not tested):
Skill.objects.filter(name__in=[sn.lower() for sn in skill_names])
CodePudding user response:
I had similar problem and this helped me
from operator import or_
from functools import reduce
from django.db.models import Q
skill_names = ['Code Quality', 'Analytical Thinking', 'Productivity', 'Communication']
skill_filter = reduce(
or_, (Q(name__icontains=skill) for skill in skill_names)
)
Skill.objects.filter(name__in=skill_filter)