﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
20555	Subwidgets not passed id attribute	mdj2@…	Matt Johnson	"If an id is not defined in a widget's attrs dictionary, and the form.auto_id variable is True, the BoundField class will generate an id for the widget automatically (via the auto_id property). The id is typically used when rendering the markup for the input element and the label tag.

If a BoundField has subwidgets, the automatically generated id is NOT passed to the subwidget. This makes it difficult to construct a label tag that references the subwidget. Consider this example:


{{{
{% for subwidget in form.some_field_with_subwidgets %}
    {{ subwidget.tag }} <label for=""????"">{{ subwidget.choice_label }}</label>
{% endfor %}
}}}


As far as I'm aware, there is no way to access the subwidget's automatically generated id for use in the label's ""for"" attribute. One must resort to something like:

<label for=""{{ form.form.some_field_with_subwidgets.auto_id }}_{{ forloop.counter0 }}"">

To fix this, I replaced BoundField.__iter__ with:


{{{
    def __iter__(self):
        """"""
        Yields rendered strings that comprise all widgets in this BoundField.

        This really is only useful for RadioSelect widgets, so that you can
        iterate over individual radio buttons in a template.
        """"""
        id_ = self.field.widget.attrs.get('id') or self.auto_id
        attrs = {'id': id_} if id_ else {}
        for subwidget in self.field.widget.subwidgets(self.html_name, self.value(), attrs=attrs):
            yield subwidget

}}}

This allows me to do something like:

{{{
{% for subwidget in form.some_field_with_subwidgets %}
    {{ subwidget.tag }} <label for=""{{ subwidget.attrs.id }}"">{{ subwidget.choice_label }}</label>
{% endfor %}
}}}"	New feature	closed	Forms	1.6-alpha-1	Normal	fixed		bmispelon@…	Accepted	1	0	0	0	1	0
