我正在尝试在Django OR
函数中使用filter()
运算符。现在我有
contactlist = Contact.objects.filter(last_name__icontains=request.POST['query'])
但我也希望按名字搜索。例如:
contactlist = Contact.objects.filter(last_name__icontains=request.POST['query'] OR first_name__icontains=request.POST['query'])
有谁知道怎么做?
答案 0 :(得分:88)
from django.db.models import Q
Contact.objects.filter(Q(last_name__icontains=request.POST['query']) |
Q(first_name__icontains=request.POST['query']))
答案 1 :(得分:10)
result = Contact.objects.filter(last_name__icontains=request.POST['query']) | Contact.objects.filter(first_name__icontains=request.POST['query'])