From 5ec64f96b2d83ec3c0ef574f52e4767a440017b8 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Sun, 8 May 2016 09:58:34 +0000 Subject: [PATCH] Fixed #26734 -- Made iterator class configurable on ModelChoiceField. --- django/forms/models.py | 3 ++- tests/model_forms/tests.py | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/django/forms/models.py b/django/forms/models.py index 0738421d9e..ff34ebfe78 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1128,6 +1128,7 @@ class ModelChoiceField(ChoiceField): 'invalid_choice': _('Select a valid choice. That choice is not one of' ' the available choices.'), } + iterator = ModelChoiceIterator def __init__(self, queryset, empty_label="---------", required=True, widget=None, label=None, initial=None, @@ -1195,7 +1196,7 @@ class ModelChoiceField(ChoiceField): # accessed) so that we can ensure the QuerySet has not been consumed. This # construct might look complicated but it allows for lazy evaluation of # the queryset. - return ModelChoiceIterator(self) + return self.iterator(self) choices = property(_get_choices, ChoiceField._set_choices) diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 193b5e1fda..78e7399637 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -14,8 +14,8 @@ from django.core.validators import ValidationError from django.db import connection, models from django.db.models.query import EmptyQuerySet from django.forms.models import ( - ModelFormMetaclass, construct_instance, fields_for_model, model_to_dict, - modelform_factory, + ModelChoiceIterator, ModelFormMetaclass, construct_instance, + fields_for_model, model_to_dict, modelform_factory, ) from django.template import Context, Template from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature @@ -1573,6 +1573,23 @@ class ModelChoiceFieldTests(TestCase): with self.assertNumQueries(1): template.render(Context({'field': field})) + def test_modelchoicefield_iterator(self): + """ + Iterator defaults to ModelChoiceIterator and can be overridden with + the iterator attribute on a ModelChoiceField subclass. + """ + field = forms.ModelChoiceField(Category.objects.all()) + self.assertIsInstance(field.choices, ModelChoiceIterator) + + class CustomModelChoiceIterator(ModelChoiceIterator): + pass + + class CustomModelChoiceField(forms.ModelChoiceField): + iterator = CustomModelChoiceIterator + + field = CustomModelChoiceField(Category.objects.all()) + self.assertIsInstance(field.choices, CustomModelChoiceIterator) + class ModelMultipleChoiceFieldTests(TestCase): def setUp(self):