From c19407cb4062571b6772a02a7875e036e5691a95 Mon Sep 17 00:00:00 2001 From: jpic <jamespic@gmail.com> Date: Tue, 9 Feb 2016 00:07:10 +0100 Subject: [PATCH] Added autocomplete for Proposal --- representatives_votes/autocompletes.py | 19 +++++++++++++++++++ representatives_votes/urls.py | 12 ++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 representatives_votes/autocompletes.py diff --git a/representatives_votes/autocompletes.py b/representatives_votes/autocompletes.py new file mode 100644 index 0000000..ee1e817 --- /dev/null +++ b/representatives_votes/autocompletes.py @@ -0,0 +1,19 @@ +from dal import autocomplete + +from django.db.models import Q + +from models import Proposal + + +class ProposalAutocomplete(autocomplete.Select2QuerySetView): + def get_queryset(self): + qs = Proposal.objects.all() + + if self.q: + qs = qs.filter( + Q(dossier__title__icontains=self.q) | + Q(title__icontains=self.q) | + Q(reference__icontains=self.q) + ) + + return qs diff --git a/representatives_votes/urls.py b/representatives_votes/urls.py index fc32da7..e6e2b74 100644 --- a/representatives_votes/urls.py +++ b/representatives_votes/urls.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.conf.urls import url import views @@ -14,3 +15,14 @@ urlpatterns = [ name='dossier-list' ), ] + +if 'dal_select2' in settings.INSTALLED_APPS: + from autocompletes import ProposalAutocomplete # noqa + + urlpatterns.append( + url( + 'autocomplete/proposal/$', + ProposalAutocomplete.as_view(), + name='proposal-autocomplete', + ), + ) -- GitLab