diff --git a/AUTHORS b/AUTHORS index a930a0e9f2..771aee8e3e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -183,6 +183,7 @@ answer newbie questions, and generally made Django that much better: Milton Waddams wam-djangobug@wamber.net Dan Watson + Chris Wesseling Rachel Willmer Gary Wilson wojtek diff --git a/django/newforms/fields.py b/django/newforms/fields.py index 0f082b9ee3..8e3da03470 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -339,8 +339,9 @@ class ChoiceField(Field): def _set_choices(self, value): # Setting choices also sets the choices on the widget. - self._choices = value - self.widget.choices = value + # choices can be any iterable, but we call list() on it because + # it will be consumed more than once. + self._choices = self.widget.choices = list(value) choices = property(_get_choices, _set_choices) diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index c4502b2202..4183002809 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -318,6 +318,7 @@ The value is compared to its str(): The 'choices' argument can be any iterable: +>>> from itertools import chain >>> def get_choices(): ... for i in range(5): ... yield (i, i) @@ -329,6 +330,17 @@ The 'choices' argument can be any iterable: +>>> things = ({'id': 1, 'name': 'And Boom'}, {'id': 2, 'name': 'One More Thing!'}) +>>> class SomeForm(Form): +... somechoice = ChoiceField(choices=chain((('', '-'*9),), [(thing['id'], thing['name']) for thing in things])) +>>> f = SomeForm() +>>> f.as_table() +u'' +>>> f.as_table() +u'' +>>> f = SomeForm({'somechoice': 2}) +>>> f.as_table() +u'' You can also pass 'choices' to the constructor: >>> w = Select(choices=[(1, 1), (2, 2), (3, 3)])