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
Stumpy Joe Pete's answer is pretty spot on, but as he mentioned, the template if query or page.object_list
check is a little hacked. A better way of solving this would be to create your own SearchForm
which would still find something if q
is empty - will not repost that - AND to customize the SearchView
with something like:
class MySearchView(SearchView):
def get_query(self):
query = []
if self.form.is_valid():
for field in self.form:
if field.name in self.form.cleaned_data and self.form.cleaned_data[field.name]:
query.append(field.name+'='+str(self.form.cleaned_data[field.name]))
return ' AND '.join(query)
In most cases, you won't even be using the query
value, so you could just as well do a quick check if any of the fields is set and return True
or something like that.. or of course you can modify the output any way you want (I'm not even 100% sure my solution would work for all field types, but you get the idea).