Django Haystack - Show results without needing a search query?

后端 未结 5 2232
情书的邮戳
情书的邮戳 2021-02-08 10:46

I would like to display all results which match selected facets even though a search query has not been inserted. Similar to how some shop applications work e.g. Amazon

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-08 11:02

    Following form display all the result if not query string is present. Now you can add custom filters.

    from your_app.forms import NonEmptySearchForm
    url(r'^your_url$', 
    SearchView(template='search.html',searchqueryset=sqs,form_class=NonEmptySearchForm), name='haystack_search'),
    

    forms.py

    #Overridding because the default sqs is always none if no query string is present
    class NonEmptySearchForm(SearchForm):
        def search(self):
            if not self.is_valid():
                return self.no_query_found()
            sqs = self.searchqueryset.auto_query(self.cleaned_data['q'])
            if self.load_all:
                sqs = sqs.load_all()
            return sqs
    

提交回复
热议问题