Home > Software engineering >  django search where database value is part equal to search value
django search where database value is part equal to search value

Time:05-17

value_in_database = 'Computer Solutions'
search_value = 'Solutions'

I know that if I have values like the above, I can find my object like this:

model.objects.filter(name__icontains=search_value)

But I was wondering if the opposite is possible in Django?

value_in_database = 'Solutions'
search_value = 'Computer Solutions'

model.objects.filter(name=search_value)

CodePudding user response:

You can try this solution and you will get all the models that its name contains "Computer" or "Solutions":

import operator
from django.db.models import Q
from functools import reduce

search_value = 'Computer Solutions'
model.objects.filter(reduce(operator.or_, (Q(name__icontains=x) for x in search_value.split(" "))))
  • Related