Ticket #6458: make_radioselect_iterable.diff

File make_radioselect_iterable.diff, 3.3 KB (added by oyvind, 17 years ago)
  • django/newforms/forms.py

     
    255255        return self.form.errors.get(self.name, self.form.error_class())
    256256    errors = property(_errors)
    257257
    258     def as_widget(self, widget=None, attrs=None):
    259         """
    260         Renders the field by rendering the passed widget, adding any HTML
    261         attributes passed as attrs.  If no widget is specified, then the
    262         field's default widget will be used.
    263         """
    264         if not widget:
    265             widget = self.field.widget
     258    def get_data_and_attrs(self, widget, attrs):
    266259        attrs = attrs or {}
    267260        auto_id = self.auto_id
    268261        if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
     
    273266                data = data()
    274267        else:
    275268            data = self.data
     269        return data, attrs
     270
     271    def as_widget(self, widget=None, attrs=None):
     272        """
     273        Renders the field by rendering the passed widget, adding any HTML
     274        attributes passed as attrs.  If no widget is specified, then the
     275        field's default widget will be used.
     276        """
     277        if not widget:
     278            widget = self.field.widget
     279        data, attrs = self.get_data_and_attrs(widget, attrs)
    276280        return widget.render(self.html_name, data, attrs=attrs)
    277281
    278282    def as_text(self, attrs=None):
     
    331335            return self.html_name
    332336        return ''
    333337    auto_id = property(_auto_id)
     338
     339    def __iter__(self):
     340        data, attrs = self.get_data_and_attrs(widget, attrs)
     341        for i in self.field.widget.iter(self.html_name, data, attrs=attrs):
     342            yield i
     343
  • django/newforms/widgets.py

     
    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

     
    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