diff --git a/api/views/autocomplete.py b/api/views/autocomplete.py index 121ddefc..a97fbbab 100644 --- a/api/views/autocomplete.py +++ b/api/views/autocomplete.py @@ -5,16 +5,20 @@ from django.db.models import Q import seminar.models as m from .helpers import LoginRequiredAjaxMixin +# TODO filosofie - zkratky, jak v databázi, tak ve vyhledávání (SPŠE, GASOŠ, Kpt., soukr) class SkolaAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): # Don't forget to filter out results depending on the visitor ! qs = m.Skola.objects.all() if self.q: - qs = qs.filter( - Q(nazev__istartswith=self.q)| - Q(kratky_nazev__istartswith=self.q)| - Q(ulice__istartswith=self.q)| - Q(mesto__istartswith=self.q)) + words = self.q.split(' ') #TODO re split podle bileho znaku + partq = Q() + for w in words: # Hledej po slovech, zahoď čárky a tečky z konců. + if w[-1] in (".",","): + w = w[:-1] + + partq &= (Q(nazev__icontains=w)|Q(kratky_nazev__icontains=w)|Q(ulice__icontains=w)|Q(mesto__icontains=w)) + qs = qs.filter(partq) return qs