2

我是 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'。我遵循了文档示例。我无法弄清楚,问题是什么?search 方法返回列表对象而不是 SearchQuerySet 对象,因此无法找到过滤器属性。请帮忙。我正在使用 python 2.7.10、Django 1.8.4 和 solr 4.10.2 sqs 是

在此处输入图像描述

4

2 回答 2

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
  • 干草堆版本:2.8.0

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

Step1: 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('.')

于 2018-03-13T09:32:20.347 回答
0

步骤1:

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('.')
step3:查找并替换
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)

于 2021-01-18T06:53:44.640 回答