Skip to content

Commit

Permalink
Merge pull request #192 from PetrDlouhy/master
Browse files Browse the repository at this point in the history
Split search term by whitespaces
  • Loading branch information
mlavin committed Jan 26, 2018
2 parents 9148f04 + 7078d4d commit 494013a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/lookups.rst
Expand Up @@ -63,6 +63,15 @@ Lookup API
:param item: An item from the search results.
:return: A string representation of the item to be returned by the field/widget.


.. py:method:: LookupBase.split_term(term)
Split searching term into array of subterms that will be searched separately.
You can override this function to achieve different splitting of the term.

:param term: The search term.
:return: Array with subterms

.. py:method:: LookupBase.get_item_value(item)
This is last of three formatting methods. The value is shown in the
Expand Down
16 changes: 12 additions & 4 deletions selectable/base.py
Expand Up @@ -36,6 +36,13 @@ def _name(cls):
return name
name = classmethod(_name)

def split_term(self, term):
"""
Split searching term into array of subterms
that will be searched separately.
"""
return term.split()

def _url(cls):
return reverse('selectable-lookup', args=[cls.name()])
url = classmethod(_url)
Expand Down Expand Up @@ -123,11 +130,12 @@ class ModelLookup(LookupBase):
def get_query(self, request, term):
qs = self.get_queryset()
if term:
search_filters = []
if self.search_fields:
for field in self.search_fields:
search_filters.append(Q(**{field: term}))
qs = qs.filter(reduce(operator.or_, search_filters))
for t in self.split_term(term):
search_filters = []
for field in self.search_fields:
search_filters.append(Q(**{field: t}))
qs = qs.filter(reduce(operator.or_, search_filters))
return qs

def get_queryset(self):
Expand Down

0 comments on commit 494013a

Please sign in to comment.