我是 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 是