gpt4 book ai didi

python - 如何根据用户输入更改 Django 查询查找

转载 作者:行者123 更新时间:2023-11-30 23:00:51 25 4
gpt4 key购买 nike

我正在为我的网站制作一个搜索优化器,并且当前正在进行一个查询,该查询会根据用户填写的表单返回相关列表。问题是表单字段是可选的,我不知道如何制作我的查找根据输入的字段而变化。有人可以建议一种有效的方法来做到这一点吗?干杯!

查看-

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()

job_listings = JobListing.objects.exclude(active_listing=False)

if business_industry == '' and business_address_region == '' and keywords == '' and pay_rate == '' and employment_type == '':
print("Nothing has been entered.")
else:
print(job_listings)
print("Something has been entered")
job_listings = JobListing.objects.filter(
Q(job_description__icontains=keywords) &
Q(business_industry=business_industry) &
Q(business_address_region__icontains=business_address_region)
).distinct()

context_dict = {
'joblistings': job_listings,
'form': form
}

return render(request, 'browse.html', context_dict)

最佳答案

我会选择一个简单的解决方案

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))

关于python - 如何根据用户输入更改 Django 查询查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35112217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com