diff options
| author | Peter Inglesby <peter.inglesby@gmail.com> | 2014-10-27 20:21:59 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-11-04 11:23:58 -0500 |
| commit | 74e1980cf96eb45079bef464fabdcbe0a6db2423 (patch) | |
| tree | 0541e693623c87c9789f1beb37d7f5831426c74b /django | |
| parent | e0685368c6b122404cf0817d5cb17ab1b211cf6d (diff) | |
Fixed #13181 -- Added support for callable choices to forms.ChoiceField
Thanks vanschelven and expleo for the initial patch.
Diffstat (limited to 'django')
| -rw-r--r-- | django/forms/fields.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index 6403509fbb..81b3e2f4f9 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -798,6 +798,15 @@ class NullBooleanField(BooleanField): return initial != data +class CallableChoiceIterator(object): + def __init__(self, choices_func): + self.choices_func = choices_func + + def __iter__(self): + for e in self.choices_func(): + yield e + + class ChoiceField(Field): widget = Select default_error_messages = { @@ -822,7 +831,12 @@ class ChoiceField(Field): # Setting choices also sets the choices on the widget. # 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) + if callable(value): + value = CallableChoiceIterator(value) + else: + value = list(value) + + self._choices = self.widget.choices = value choices = property(_get_choices, _set_choices) |
