我正在为我的网站制作搜索精炼程序,我正在进行查询,根据用户填写的表单返回相关的列表。问题是表单字段是可选的,我无法弄清楚如何制作我的查找根据输入的字段而变化。有人可以建议一个有效的方法吗?干杯!
查看 -
INSERT INTO Users VALUES ("User1","somedate");
答案 0 :(得分:2)
我会选择一个简单的解决方案
job_listings = []
if business_industry: job_listings += JobListing.objects.filter(business_industry=business_industry).list()
if keywords: job_listings += JobListing.objects.filter(job_description__icontains=keywords).list()
if business_address_region : job_listings += JobListing.objects.filter(business_address_region__icontains=business_address_region).list()
job_listing = list(set(job_listing))
答案 1 :(得分:1)
def browse(request):
business_industry = request.GET.get('business_industry', '')
business_address_region = request.GET.get('business_address_region', '')
employment_type = request.GET.get('employment_type', '')
pay_rate = request.GET.get('pay_rate', '')
keywords = request.GET.get('keywords', '')
form = JobSearchForm()
filters = Q(active_listing=True)
if business_industry:
filters &= Q(business_industry=business_industry)
if business_address_region:
filters &= Q(business_address_region__icontains=business_address_region)
if keywords:
filters &= Q(job_description__icontains=keywords)
job_listings = JobListing.objects.filter(filters).distinct()
context_dict = {
'joblistings': job_listings,
'form': form
}
return render(request, 'browse.html', context_dict)