haystack搜索没有正确返回结果,错误:list'对象没有属性' filter'

时间:2015-12-01 12:26:52

标签: django solr django-haystack django-search-lucene

我是django和haystack的菜鸟, 这是我的表格,它扩展了haystack SearchForm

from haystack.forms import SearchForm
from ksaprice_app.models import ProductDiff, Vendor
from django import forms
from haystack.query import SearchQuerySet
class ProductsSearchForm(SearchForm):
    vendor_list=Vendor.objects.all().values_list('vendor_name')
    OPTIONS=( (item,str(item)) for item in vendor_list)
    print OPTIONS    select_vendor=forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,choices=OPTIONS, required=False)
    def __init__(self, *args, **kwargs):
        super(ProductsSearchForm, self).__init__(*args, **kwargs)
    def search(self):
      sqs=super(ProductsSearchForm, self).search()
      if not self.is_valid():
        return self.no_query_found()
    #check to see if the submitted is clean and then filter
      if self.is_valid():               
        if self.cleaned_data['select_vendor']:
            #name of the field in html will be by default form field name
            vendor_filter=self.cleaned_data['select_vendor']
            #v_filter=vendor_filter[0].replace("(u'","").replace("',)","")
            v_filter=vendor_filter[0]
            print v_filter
            #sqs=SearchQuerySet().filter(product_vendor_name=v_filter)[:10]
            sqs=sqs.filter(product_vendor_name=v_filter)[:10] ##error is here
    return sqs
    def no_query_found(self):
        return self.searchqueryset.all()[:10]

这是视图

def ProductsSearch(request):
#instantiate the form with parameters from get method
  form = forms.ProductsSearchForm(request.GET)
  #calling search method here that fetches the search result
  context_var={}
  context_var['products'] = form.search()
  context_var['form']=forms.ProductsSearchForm
  print context_var
  return render_to_response('ksaprice_app/product_search.html', {'context_var': context_var})

在sqs.filter,我收到此错误'list' object has no attribute 'filter'。我关注了documentationexample。我无法弄清楚,问题是什么? search方法返回list对象而不是SearchQuerySet对象,因此无法找到filter属性。请帮忙。 我使用的是python 2.7.10,Django 1.8.4和solr 4.10.2 sqs是

enter image description here

2 个答案:

答案 0 :(得分:1)

3-4天后,我到达了解决方案。

解决方案1-:架构更改

第1步:转到您的收藏管理架构文件并进行修改 第2步:查找并将<field name="django_ct" type="text_general"/>替换为<field name="django_ct" type="string"/>

  • 操作系统版本:Ubuntu 14.04 LTS
  • 搜索引擎版本:Solr 7.2.1
  • Python版本:3.4.3
  • Django版本:2.0
  • Haystack版本:2.8.0

解决方案2-:破解核心文件

Step1: sudo vim / home / your path / site-packages / haystack / backends / solr_backend.py 第2步:查找并将app_label, model_name = raw_result[DJANGO_CT].split('.')替换为app_label, model_name = raw_result[DJANGO_CT][0].split('.')

答案 1 :(得分:0)

第一步:

sudo vim /home/your path/site-packages/haystack/backends/solr_backend.py

Step2:查找和替换
app_label, model_name = raw_result[DJANGO_CT].split('.')

app_label, model_name = raw_result[DJANGO_CT][0].split('.')
第三步:查找和替换
result = result_class(app_label, model_name, raw_result[DJANGO_ID], raw_result['score'], **additional_fields)

result = result_class(app_label, model_name, raw_result[DJANGO_ID][0], raw_result['score'], **additional_fields)