我希望能够像这样输入字符串列表:
["searchterm1", "searchterm2", "searchtermN"]
输出所有行,其中"searchterm1"
匹配其中一列 OR "searchterm2"
匹配其中一列,依此类推。
有办法吗?
答案 0 :(得分:1)
from django.db.models import Q
def search(request):
list = ["searchterm1", "searchterm2", "searchtermN"]
q_object = Q(title__icontains=list[0]) | Q(description__icontains=list[0])
for item in list:
q_object.add((Q(title__icontains=item) | Q(description__icontains=item)), q_object.connector)
queryset = Product.objects.filter(q_objects)