I wanted a unique id for each <li> when rendering a RadioSelect widget. This turns out to be quite doable with Django/Python by subclassing the RadioSelect widget and it's renderer.
class CustomRadioRenderer(forms.widgets.RadioFieldRenderer):
def render(self):
"""Outputs a <ul> for this set of radio fields."""
return mark_safe(u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li id="%s">%s</li>'
% ('radio-%s'%w.index, force_unicode(w) ) for w in self]))
class CustomRadioSelect(forms.widgets.RadioSelect):
renderer = CustomRadioRenderer
Now, where I define my form, instead of:
class SelectCommChoices(forms.ModelForm):
commercial_type = forms.ChoiceField(
label="Select Type of Account",
widget=forms.widgets.RadioSelect(),
choices=commercial_service_choices,
required=True,
help_text="Select the type of commercial account",
)
class Meta:
model = UserProfile
fields = ('commercial_type',)
Where I have widget=forms.widgets.RadioSelect(), I can replace with widget=CustomRadioSelect() and each <li> will have it's unique css id, radio-0, radio-1, etc.