Hi, 
I'm stuck and I'm hoping someone here can point me in the right direction.

I have a SearchForm that lets the user filter their search results when 
they select certain choices from available fields.

As you can see by the code below, instead of getting the CHOICES via the 
SearchQuerySet, I'm using Django's QuerySet instead. Further down the code, 
I'm using haystack SearchQuerySet to return the results.

What I'm having trouble with is modifying this to use the SearchQuerySet 
for CHOICES, so that each selection will narrow the available choices based 
on the SQS.

Any help is much appreciated.

class CustomSearchForm(SearchForm):
    def no_query_found(self):
        return self.searchqueryset.all()


    retailers = sorted(tuple(set([(choice, choice) for choice in 
        Retailer.objects.order_by('retailer_name').values_list('retailer_name', 
flat=True)])))

    brands = sorted(tuple(set([(choice, choice) for choice in 
        
Brand.objects.exclude(brand_name__isnull=True).order_by('brand_name').values_list('brand_name',
 flat=True)])))

    categories = sorted(tuple(set([(choice, choice) for choice in 
        Category.objects.order_by('category_name').values_list('category_name', 
flat=True)])))

    colors = sorted(tuple(set([(choice, choice) for choice in 
        Color.objects.values_list('color_true', flat=True)])))

    sizes = sorted(tuple(set([(choice, choice) for choice in 
        Size.objects.values_list('size_true', flat=True)])))


    sorting = (('1','Random'),('2','Last Updated (most recent)'),('3','Regular 
Price'),('4','Sale Price'),)

    sort = forms.ChoiceField(choices = sorting, label="Sort 
By",label_suffix='',initial='1', widget=forms.Select(attrs = {'onchange' : 
"this.form.submit();", }), required=False)
    onsale = forms.BooleanField(required=False,widget=forms.CheckboxInput(attrs 
= {'onclick' : "this.form.submit();", }), label='On Sale', label_suffix='')
    retailer = 
forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs = 
{'onclick' : "this.form.submit();", }), choices=retailers, 
label_suffix='',required = False)
    brand = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs 
= {'onclick' : "this.form.submit();", }), choices=brands, 
label_suffix='',required = False)
    category = 
forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs = 
{'onclick' : "this.form.submit();", }), choices=categories, 
label_suffix='',required = False)
    color = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs 
= {'onclick' : "this.form.submit();", }), choices=colors, 
label_suffix='',required = False)
    size = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs 
= {'onclick' : "this.form.submit();", }), choices=sizes, 
label_suffix='',required = False)
    q = forms.CharField(required=False,widget=forms.TextInput(attrs={'type': 
'search','placeholder':'Search'}), label='')


    def search(self):

        sqs = super(CustomSearchForm, self).search()

        if not self.is_valid():
            return self.no_query_found()

        if self.cleaned_data['retailer']:
            sqs = sqs.filter_and(retailer__in = self.cleaned_data['retailer'])

        if self.cleaned_data['brand']:
            sqs = sqs.filter_and(brand__in = self.cleaned_data['brand'])

        if self.cleaned_data['category']:
            sqs = sqs.filter_and(category__in = self.cleaned_data['category'])

        if self.cleaned_data['color']:
            sqs = sqs.filter_and(color__in = self.cleaned_data['color']) 

        if self.cleaned_data['size']:
            sqs = sqs.filter_and(size__in = self.cleaned_data['size']) 

        if self.cleaned_data['onsale']:
            sqs = sqs.filter_and(is_sale__exact = self.cleaned_data['onsale'])

        if self.cleaned_data['sort'] == '1':
            sqs = sqs.order_by('?')     
        if self.cleaned_data['sort'] == '2':
            sqs = sqs.order_by('-last_updated')
        if self.cleaned_data['sort'] == '3':
            sqs = sqs.order_by('-price')
        if self.cleaned_data['sort'] == '4':
            sqs = sqs.order_by('-sale_price')

        return sqs

-- 
You received this message because you are subscribed to the Google Groups 
"django-haystack" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-haystack+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to