From 7923eeb0ee5367459cb90f33e059ee96b88990ad Mon Sep 17 00:00:00 2001 From: Johannes Hoppe Date: Mon, 8 Feb 2016 09:18:59 +0100 Subject: [PATCH] Removes choices from render and render_options signature Choices has been removed by @jpic in https://github.com/django/django/commit/926e90132dc15d76bb8d16e2f9f1279566cac3c3 --- django_select2/forms.py | 28 ++++++++++++++++++++-------- tests/test_forms.py | 9 ++++----- tests/testapp/settings.py | 9 ++++++--- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/django_select2/forms.py b/django_select2/forms.py index 29689a1..15185ca 100644 --- a/django_select2/forms.py +++ b/django_select2/forms.py @@ -87,10 +87,10 @@ class Select2Mixin(object): attrs['class'] = 'django-select2' return attrs - def render_options(self, choices, selected_choices): + def render_options(self, *args, **kwargs): """Render options including an empty one, if the field is not required.""" output = '' if not self.is_required else '' - output += super(Select2Mixin, self).render_options(choices, selected_choices) + output += super(Select2Mixin, self).render_options(*args, **kwargs) return output def _get_media(self): @@ -211,9 +211,9 @@ class HeavySelect2Mixin(object): attrs['class'] += ' django-select2-heavy' return attrs - def render(self, name, value, attrs=None, choices=()): + def render(self, *args, **kwargs): """Render widget and register it in Django's cache.""" - output = super(HeavySelect2Mixin, self).render(name, value, attrs=attrs, choices=choices) + output = super(HeavySelect2Mixin, self).render(*args, **kwargs) self.set_to_cache() return output @@ -227,9 +227,15 @@ class HeavySelect2Mixin(object): 'url': self.get_url(), }) - def render_options(self, choices, selected_choices): + def render_options(self, *args): """Render only selected options.""" - choices = chain(choices, self.choices) + try: + selected_choices, = args + except ValueError: # Signature contained `choices` prior to Django 1.10 + choices, selected_choices = args + choices = chain(self.choices, choices) + else: + choices = self.choices output = ['' if not self.is_required else ''] choices = {(k, v) for k, v in choices if k in selected_choices} selected_choices = {force_text(v) for v in selected_choices} @@ -384,8 +390,15 @@ class ModelSelect2Mixin(object): return self.search_fields raise NotImplementedError('%s, must implement "search_fields".' % self.__class__.__name__) - def render_options(self, choices, selected_choices): + def render_options(self, *args): """Render only selected options and set QuerySet from :class:`ModelChoicesIterator`.""" + try: + selected_choices, = args + except ValueError: + choices, selected_choices = args + choices = chain(self.choices, choices) + else: + choices = self.choices output = ['' if not self.is_required else ''] if isinstance(self.choices, ModelChoiceIterator): if not self.queryset: @@ -395,7 +408,6 @@ class ModelSelect2Mixin(object): choices = {(obj.pk, self.label_from_instance(obj)) for obj in self.choices.queryset.filter(pk__in=selected_choices)} else: - choices = chain(choices, self.choices) choices = {(k, v) for k, v in choices if k in selected_choices} selected_choices = {force_text(v) for v in selected_choices} for option_value, option_label in choices: diff --git a/tests/test_forms.py b/tests/test_forms.py index 8569fb1..2cbfa14 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -123,14 +123,13 @@ class TestHeavySelect2Mixin(TestSelect2Mixin): not_required_field = self.form.fields['primary_genre'] assert not_required_field.required is False assert '' in \ - not_required_field.widget.render('primary_genre', 1, choices=NUMBER_CHOICES), \ - not_required_field.widget.render('primary_genre', 1, choices=NUMBER_CHOICES) + not_required_field.widget.render('primary_genre', 1), \ + not_required_field.widget.render('primary_genre', 1) def test_many_selected_option(self, db, genres): field = HeavySelect2MultipleWidgetForm().fields['genres'] - widget_output = field.widget.render( - 'genres', [1, 2], - choices=NUMBER_CHOICES) + field.widget.choices = NUMBER_CHOICES + widget_output = field.widget.render('genres', [1, 2]) selected_option = ''.format(pk=1, value='One') selected_option2 = ''.format(pk=2, value='Two') diff --git a/tests/testapp/settings.py b/tests/testapp/settings.py index c9d9256..63d1546 100644 --- a/tests/testapp/settings.py +++ b/tests/testapp/settings.py @@ -33,9 +33,12 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media') SITE_ID = 1 ROOT_URLCONF = 'tests.testapp.urls' -TEMPLATE_DIRS = ( - os.path.join(BASE_DIR, "templates"), -) +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + }, +] SECRET_KEY = '123456'