Code

Ticket #6458: make_radioselect_iterable2.diff

File make_radioselect_iterable2.diff, 3.7 KB (added by J. Pablo Fernández <pupeno@…>, 6 years ago)

This patch solves the issue in my last comment and also ads value indexing.

Line 
1diff --git a/django/newforms/forms.py b/django/newforms/forms.py
2index 2c481e4..fac6915 100644
3--- a/django/newforms/forms.py
4+++ b/django/newforms/forms.py
5@@ -272,12 +272,7 @@ class BoundField(StrAndUnicode):
6         return self.form.errors.get(self.name, self.form.error_class())
7     errors = property(_errors)
8 
9-    def as_widget(self, widget=None, attrs=None):
10-        """
11-        Renders the field by rendering the passed widget, adding any HTML
12-        attributes passed as attrs.  If no widget is specified, then the
13-        field's default widget will be used.
14-        """
15+    def get_data_attrs_and_widget(self, widget, attrs):
16         if not widget:
17             widget = self.field.widget
18         attrs = attrs or {}
19@@ -290,6 +285,15 @@ class BoundField(StrAndUnicode):
20                 data = data()
21         else:
22             data = self.data
23+        return data, attrs, widget
24+
25+    def as_widget(self, widget=None, attrs=None):
26+        """
27+        Renders the field by rendering the passed widget, adding any HTML
28+        attributes passed as attrs.  If no widget is specified, then the
29+        field's default widget will be used.
30+        """
31+        data, attrs, widget = self.get_data_attrs_and_widget(widget, attrs)
32         return widget.render(self.html_name, data, attrs=attrs)
33 
34     def as_text(self, attrs=None):
35@@ -348,3 +352,19 @@ class BoundField(StrAndUnicode):
36             return self.html_name
37         return ''
38     auto_id = property(_auto_id)
39+
40+    def __iter__(self):
41+        data, attrs, _ = self.get_data_attrs_and_widget(None, None) #widget, attrs)
42+        for i in self.field.widget.iter(self.html_name, data, attrs=attrs):
43+            yield i
44+
45+    def __getitem__(self, i):
46+        items_list = []
47+        items_dict = {}
48+        for item in self:
49+            items_list.append(item)
50+            items_dict[item.choice_value] = item
51+        try:
52+            return items_list[i]
53+        except TypeError:
54+            return items_dict[i]
55diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py
56index 20a7cab..3dc89e1 100644
57--- a/django/newforms/widgets.py
58+++ b/django/newforms/widgets.py
59@@ -341,6 +341,10 @@ class RadioSelect(Select):
60     def render(self, name, value, attrs=None, choices=()):
61         return self.get_renderer(name, value, attrs, choices).render()
62 
63+    def iter(self, name, value, attrs=None, choices=()):
64+        for i in self.get_renderer(name, value, attrs, choices):
65+            yield i
66+
67     def id_for_label(self, id_):
68         # RadioSelect is represented by multiple <input type="radio"> fields,
69         # each of which has a distinct ID. The IDs are made distinct by a "_X"
70diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
71index 0e69602..d75a468 100644
72--- a/tests/regressiontests/forms/widgets.py
73+++ b/tests/regressiontests/forms/widgets.py
74@@ -757,6 +757,14 @@ u'<ul>\n<li><label><input checked="checked" type="radio" name="email" value="\u0
75 <li><label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
76 </ul>
77 
78+>>> w = RadioSelect()
79+>>> for i in w.iter('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'}):
80+...     print i
81+<label><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label>
82+<label><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label>
83+<label><input type="radio" id="bar_2" value="G" name="beatle" /> George</label>
84+<label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label>
85+
86 # CheckboxSelectMultiple Widget ###############################################
87 
88 >>> w = CheckboxSelectMultiple()