Ticket #6458: make_radioselect_iterable2.diff

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

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

  • django/newforms/forms.py

    diff --git a/django/newforms/forms.py b/django/newforms/forms.py
    index 2c481e4..fac6915 100644
    a b class BoundField(StrAndUnicode):  
    272272        return self.form.errors.get(self.name, self.form.error_class())
    273273    errors = property(_errors)
    274274
    275     def as_widget(self, widget=None, attrs=None):
    276         """
    277         Renders the field by rendering the passed widget, adding any HTML
    278         attributes passed as attrs.  If no widget is specified, then the
    279         field's default widget will be used.
    280         """
     275    def get_data_attrs_and_widget(self, widget, attrs):
    281276        if not widget:
    282277            widget = self.field.widget
    283278        attrs = attrs or {}
    class BoundField(StrAndUnicode):  
    290285                data = data()
    291286        else:
    292287            data = self.data
     288        return data, attrs, widget
     289
     290    def as_widget(self, widget=None, attrs=None):
     291        """
     292        Renders the field by rendering the passed widget, adding any HTML
     293        attributes passed as attrs.  If no widget is specified, then the
     294        field's default widget will be used.
     295        """
     296        data, attrs, widget = self.get_data_attrs_and_widget(widget, attrs)
    293297        return widget.render(self.html_name, data, attrs=attrs)
    294298
    295299    def as_text(self, attrs=None):
    class BoundField(StrAndUnicode):  
    348352            return self.html_name
    349353        return ''
    350354    auto_id = property(_auto_id)
     355
     356    def __iter__(self):
     357        data, attrs, _ = self.get_data_attrs_and_widget(None, None) #widget, attrs)
     358        for i in self.field.widget.iter(self.html_name, data, attrs=attrs):
     359            yield i
     360
     361    def __getitem__(self, i):
     362        items_list = []
     363        items_dict = {}
     364        for item in self:
     365            items_list.append(item)
     366            items_dict[item.choice_value] = item
     367        try:
     368            return items_list[i]
     369        except TypeError:
     370            return items_dict[i]
  • django/newforms/widgets.py

    diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py
    index 20a7cab..3dc89e1 100644
    a b class RadioSelect(Select):  
    341341    def render(self, name, value, attrs=None, choices=()):
    342342        return self.get_renderer(name, value, attrs, choices).render()
    343343
     344    def iter(self, name, value, attrs=None, choices=()):
     345        for i in self.get_renderer(name, value, attrs, choices):
     346            yield i
     347
    344348    def id_for_label(self, id_):
    345349        # RadioSelect is represented by multiple <input type="radio"> fields,
    346350        # each of which has a distinct ID. The IDs are made distinct by a "_X"
  • tests/regressiontests/forms/widgets.py

    diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
    index 0e69602..d75a468 100644
    a b u'<ul>\n<li><label><input checked="checked" type="radio" name="email" value="\u0  
    757757<li><label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
    758758</ul>
    759759
     760>>> w = RadioSelect()
     761>>> for i in w.iter('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'}):
     762...     print i
     763<label><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label>
     764<label><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label>
     765<label><input type="radio" id="bar_2" value="G" name="beatle" /> George</label>
     766<label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label>
     767
    760768# CheckboxSelectMultiple Widget ###############################################
    761769
    762770>>> w = CheckboxSelectMultiple()
Back to Top