diff --git a/django/forms/forms.py b/django/forms/forms.py index 7818aac13a080..d3f2046779f24 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -418,7 +418,13 @@ def __iter__(self): iterate over individual radio buttons in a template. """ for subwidget in self.field.widget.subwidgets(self.html_name, self.value()): - yield self.as_widget(subwidget) + yield subwidget + + def __len__(self): + return len(list(self.__iter__())) + + def __getitem__(self, idx): + return list(self.__iter__())[idx] def _errors(self): """ diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 9c04750fdb262..49b148eeca263 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -137,6 +137,22 @@ def __new__(cls, name, bases, attrs): new_class.media = media_property(new_class) return new_class +class SubWidget(StrAndUnicode): + """ + Some widgets are made of multiple HTML elements -- namely, RadioSelect. + This is a class that represents the "inner" HTML element of a widget. + """ + def __init__(self, parent_widget, name, value, attrs, choices): + self.parent_widget = parent_widget + self.name, self.value = name, value + self.attrs, self.choices = attrs, choices + + def __unicode__(self): + args = [self.name, self.value, self.attrs] + if self.choices: + args.append(self.choices) + return self.parent_widget.render(*args) + class Widget(object): __metaclass__ = MediaDefiningClass is_hidden = False # Determines whether this corresponds to an . @@ -163,7 +179,7 @@ def subwidgets(self, name, value, attrs=None, choices=()): Arguments are the same as for render(). """ - yield self + yield SubWidget(self, name, value, attrs, choices) def render(self, name, value, attrs=None): """ @@ -623,7 +639,7 @@ def _has_changed(self, initial, data): data_set = set([force_unicode(value) for value in data]) return data_set != initial_set -class RadioInput(StrAndUnicode): +class RadioInput(SubWidget): """ An object used by RadioFieldRenderer that represents a single . diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index b657be4c53492..ac110c2ee8ff7 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -386,8 +386,41 @@ commonly used groups of widgets: - If you decide not to loop over the radio buttons, they'll be output in a - ``