Skip to content

Instantly share code, notes, and snippets.

@sweemeng
Created February 2, 2010 12:38
Show Gist options
  • Save sweemeng/292626 to your computer and use it in GitHub Desktop.
Save sweemeng/292626 to your computer and use it in GitHub Desktop.
from haystack.forms import SearchForm
from django import forms
from haystack.query import SearchQuerySet
SEARCH_FILTER =[('all','all'),('name','name'),('business','sector'),('address','address')]
class FilterSearchForm(SearchForm):
def __init__(self,*args,**kwargs):
super(FilterSearchForm,self).__init__(*args,**kwargs)
self.fields['keys'] = forms.ChoiceField(choices=SEARCH_FILTER,required=True,
label='search in')
def search(self):
d = {}
sqs = super(FilterSearchForm,self).search()
key = str(self.cleaned_data['keys'])
data = str(self.cleaned_data['q'])
d[key] = data
if key == 'all':
return sqs
else:
print d
return sqs.filter(**d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment