From 485ea191cb529d9def087f4eb61a86b8b1c8a1f7 Mon Sep 17 00:00:00 2001 From: Johannes Hoppe Date: Wed, 30 Sep 2015 10:28:37 +0200 Subject: [PATCH] Fixed filter_queryset bug The reduce statement coundn't handle multiple lookup strings. --- django_select2/__init__.py | 2 +- django_select2/forms.py | 2 +- tests/test_forms.py | 7 ++++++- tests/testapp/forms.py | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/django_select2/__init__.py b/django_select2/__init__.py index 0465bac..33d1bc8 100644 --- a/django_select2/__init__.py +++ b/django_select2/__init__.py @@ -9,4 +9,4 @@ The app includes Select2 driven Django Widgets and Form Fields. """ -__version__ = "5.0.2" +__version__ = "5.0.3" diff --git a/django_select2/forms.py b/django_select2/forms.py index e154ef8..61b492e 100644 --- a/django_select2/forms.py +++ b/django_select2/forms.py @@ -311,7 +311,7 @@ class ModelSelect2Mixin(object): """ qs = self.get_queryset() search_fields = self.get_search_fields() - select = reduce(lambda x, y: Q(**{x: term}) | Q(**{y: term}), search_fields, + select = reduce(lambda x, y: x | Q(**{y: term}), search_fields, Q(**{search_fields.pop(): term})) return qs.filter(select).distinct() diff --git a/tests/test_forms.py b/tests/test_forms.py index 13fe1ce..3b37dd4 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -19,7 +19,8 @@ from django_select2.forms import ( Select2Widget ) from tests.testapp import forms -from tests.testapp.forms import NUMBER_CHOICES, HeavySelect2MultipleWidgetForm +from tests.testapp.forms import NUMBER_CHOICES, HeavySelect2MultipleWidgetForm, \ + TitleModelSelect2Widget from tests.testapp.models import Genre @@ -148,6 +149,10 @@ class TestModelSelect2Mixin(TestHeavySelect2Mixin): assert isinstance(widget.get_search_fields(), collections.Iterable) assert all(isinstance(x, text_type) for x in widget.get_search_fields()) + def test_filter_queryset(self, genres): + widget = TitleModelSelect2Widget(queryset=Genre.objects.all()) + assert widget.filter_queryset(genres[0].title[:3]).exists() + def test_model_kwarg(self): widget = ModelSelect2Widget(model=Genre, search_fields=['title__icontains']) genre = Genre.objects.last() diff --git a/tests/testapp/forms.py b/tests/testapp/forms.py index b66a379..bf3e442 100644 --- a/tests/testapp/forms.py +++ b/tests/testapp/forms.py @@ -14,7 +14,8 @@ from tests.testapp.models import Album class TitleSearchFieldMixin(object): search_fields = [ - 'title__icontains' + 'title__icontains', + 'pk__startswith' ]