过滤多个关键字

时间:2016-02-01 08:41:46

标签: python django

我正在尝试在我的网站上创建一个过滤关键字的搜索精简程序,问题是有时人们可能想要搜索多个关键字。问题是默认情况下,如果他们进入" hello world"它会搜索那个确切的短语,但我希望它将它分开,以便它搜索"你好"和"世界"。到目前为止,我已将__icontains添加到关键字中并确实划分了它,但它阻止我在查询中使用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', '').split() form = JobSearchForm(initial=request.GET) filters = Q(active_listing=True) if business_industry: filters &= Q(business_industry=business_industry) if business_address_region: filters &= Q(business_address_region=business_address_region) if employment_type: filters &= Q(employment_type=employment_type) if pay_rate: filters &= Q(pay_rate=pay_rate) if keywords: filters &= Q(job_description__icontains=keywords) | Q(job_title__icontains=keywords) job_listings = JobListing.objects.filter(filters).distinct().order_by('-listing_date') context_dict = { 'joblistings': job_listings, 'form': form } return render(request, 'browse.html', context_dict) 。谁能建议最好的方法呢?干杯!

代码 -

ActiveAdmin.register Expense do    

  index do       
    amount = 0
    column :amount 
    column :details
    column :created_at
    column("sub amount") {|resource| amount = amount + resource.amount}
    default_actions
    div :class => "panel" do
      h3 "Total: #{amount}"
    end
  end         
end

编辑: 我被要求解释为什么这篇文章是独一无二的,另一个问题是询问如何将他的查询与他的所有模型字段进行比较。这是在询问如何从单个字段中过滤多个关键字。

4 个答案:

答案 0 :(得分:2)

您可以循环查询keywords

job_description_q = Q()
for keyword in keywords:
    job_description_q |= Q(job_description__icontains=keyword)
filters &= job_description_q 

jobtitle的类似循环。

答案 1 :(得分:2)

您可以使用Reduce功能:

reduce(lambda x, y: x | y, [Q(name__icontains=word) for word in list])

更改此代码:

if keywords:
    filters &= Q(job_description__icontains=keywords) | Q(job_title__icontains=keywords)

致:

if keywords:
    fliters &= reduce(lambda x, y: x | y, [Q(job_description__icontains=word) for word in keywords]) | reduce(lambda x, y: x | y, [Q(job_title__icontains=word) for word in keywords])

您可以根据需要更改lambda定义。例如,如果您要查找包含所有关键字的商家信息,请将lambda x, y: x | y替换为lambda x, y: x & y

编辑: 修改了代码,因为有两个不必要的括号

答案 2 :(得分:1)

您可能需要为此

使用迭代
keyword_filter = Q()
for keyword in keywords:
   keyword_filter|=Q(job_description__icontains=keyword) | Q(job_title__icontains=keyword)
filters &= keyword_filter

您可以查看类似的问题https://stackoverflow.com/a/5956422/2599266

答案 3 :(得分:0)

filters = {'active_listing':True}
q = Q()

if business_industry:
    filters['business_industry'] = business_industry
if business_address_region:
    filters['business_address_region'] = business_address_region
if employment_type:
    filters['employment_type'] = employment_type
if pay_rate:
    filters['pay_rate'] = pay_rate
if keywords:
    q = Q(job_description__icontains=keywords) | Q(job_title__icontains=keywords)

job_listings = JobListing.objects.filter(q, **filters).distinct().order_by('-listing_date')