Filter Django QuerySet to include the results from the given list only -
i need books title contain 'parts' variable-length list.
like (not working):
title_list = ['tree', 'sun'] books = books.objects.filter(title__icontains=title_list) or in sql (working):
select * `books` title '%tree%' , title '%sun%'; how can this?
this appears work:
from django.db.models import q title_list = ['tree', 'sun'] books.objects.filter(reduce(lambda a, b: & b, [q(title__icontains=title) title in title_list])
Comments
Post a Comment