Skip to content

Commit 9c4a691

Browse files
committed
Made it easier to override SearchView/SearchForm behavior when no query is present.
No longer do you need to override both ``SearchForm`` & ``SearchView`` if you want to return all results. Use the built-in ``SearchView``, provide your own custom ``SearchForm`` subclass & override the ``no_query_found`` method per the docstring.
1 parent e55d524 commit 9c4a691

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

haystack/forms.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.utils.text import capfirst
44
from django.utils.translation import ugettext_lazy as _
55
import haystack
6-
from haystack.query import SearchQuerySet
6+
from haystack.query import SearchQuerySet, EmptySearchQuerySet
77

88

99
def model_choices(site=None):
@@ -26,16 +26,30 @@ def __init__(self, *args, **kwargs):
2626

2727
super(SearchForm, self).__init__(*args, **kwargs)
2828

29+
def no_query_found(self):
30+
"""
31+
Determines the behavior when no query was found.
32+
33+
By default, no results are returned (``EmptySearchQuerySet``).
34+
35+
Should you want to show all results, override this method in your
36+
own ``SearchForm`` subclass and do ``return self.searchqueryset.all()``.
37+
"""
38+
return EmptySearchQuerySet()
39+
2940
def search(self):
30-
if self.is_valid():
31-
sqs = self.searchqueryset.auto_query(self.cleaned_data['q'])
32-
33-
if self.load_all:
34-
sqs = sqs.load_all()
35-
36-
return sqs
37-
else:
38-
return []
41+
if not self.is_valid():
42+
return self.no_query_found()
43+
44+
if not self.cleaned_data['q']:
45+
return self.no_query_found()
46+
47+
sqs = self.searchqueryset.auto_query(self.cleaned_data['q'])
48+
49+
if self.load_all:
50+
sqs = sqs.load_all()
51+
52+
return sqs
3953

4054
def get_suggestion(self):
4155
if not self.is_valid():
@@ -70,8 +84,9 @@ def get_models(self):
7084
"""Return an alphabetical list of model classes in the index."""
7185
search_models = []
7286

73-
for model in self.cleaned_data['models']:
74-
search_models.append(models.get_model(*model.split('.')))
87+
if self.is_valid():
88+
for model in self.cleaned_data['models']:
89+
search_models.append(models.get_model(*model.split('.')))
7590

7691
return search_models
7792

haystack/views.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class SearchView(object):
2020
form = None
2121
results_per_page = RESULTS_PER_PAGE
2222

23-
2423
def __init__(self, template=None, load_all=True, form_class=ModelSearchForm, searchqueryset=None, context_class=RequestContext, results_per_page=None):
2524
self.load_all = load_all
2625
self.form_class = form_class
@@ -83,10 +82,7 @@ def get_results(self):
8382
8483
Returns an empty list if there's no query to search with.
8584
"""
86-
if self.query:
87-
return self.form.search()
88-
89-
return EmptySearchQuerySet()
85+
return self.form.search()
9086

9187
def build_page(self):
9288
"""

tests/core/tests/views.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def test_invalid_page(self):
5353

5454
def test_empty_results(self):
5555
sv = SearchView()
56+
sv.request = HttpRequest()
57+
sv.form = sv.build_form()
5658
self.assert_(isinstance(sv.get_results(), EmptySearchQuerySet))
5759

5860
def test_initial_data(self):
@@ -141,6 +143,8 @@ def test_search_no_query(self):
141143

142144
def test_empty_results(self):
143145
fsv = FacetedSearchView()
146+
fsv.request = HttpRequest()
147+
fsv.form = fsv.build_form()
144148
self.assert_(isinstance(fsv.get_results(), EmptySearchQuerySet))
145149

146150

0 commit comments

Comments
 (0)