2017-11-19 18:41:39 +01:00
|
|
|
from dal import autocomplete
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.db.models import Q
|
2017-11-19 18:41:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Select2QuerySetView(autocomplete.Select2QuerySetView):
|
|
|
|
model = None
|
|
|
|
search_fields = []
|
|
|
|
|
|
|
|
def get_queryset_filter(self):
|
|
|
|
q = self.q
|
|
|
|
filter_q = Q()
|
|
|
|
|
|
|
|
if not q:
|
|
|
|
return filter_q
|
|
|
|
|
|
|
|
words = q.split()
|
|
|
|
|
|
|
|
for word in words:
|
|
|
|
for field in self.search_fields:
|
2018-10-06 12:35:49 +02:00
|
|
|
filter_q |= Q(**{"{}__icontains".format(field): word})
|
2017-11-19 18:41:39 +01:00
|
|
|
|
|
|
|
return filter_q
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return self.model.objects.filter(self.get_queryset_filter())
|