Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update .get_search_query() to accomodate multiple values of 'q' #266

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions extra_views/contrib/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,21 @@ class SearchableListMixin(object):

def get_words(self, query):
if self.search_split:
return query.split()
return [query]

s_query = []
for w in query:
s_query += w.split()
return self.strip_list(s_query)
return self.strip_list(query)

def strip_list(self, query):
"""
Replicates previous behaviour of 'get_search_query()' where
it was expected that the returned string would have '.strip()'
applied, ie:
return self.search_use_q and self.request.GET.get("q", "").strip()
"""
return [i.strip() for i in query]

def get_search_fields_with_filters(self):
fields = []
for sf in self.search_fields:
Expand All @@ -80,9 +92,13 @@ def try_convert_to_date(self, word):
def get_search_query(self):
"""
Get query from request.GET 'q' parameter when search_use_q is set to True
Override this method to provide your own query to search
Override this method to provide your own query to search.

Updated to deliver a list via .getlist() rather than .get() in order
to accomodate multiple values of 'q', for example from
forms.MultipleChoiceField() and/or forms.CheckboxSelectMultiple widget.
"""
return self.search_use_q and self.request.GET.get("q", "").strip()
return self.search_use_q and self.request.GET.getlist("q", "")

def get_queryset(self):
qs = super(SearchableListMixin, self).get_queryset()
Expand Down