I would want to display all NullBooleanFields in my application as radio buttons. What’s the best way to do it?
Following template or something similar would be ideal. Just to allow rich styling and tone-of-voice different from plain “Yes/No/Unknown”.
'''<li class="field-%s">
<label class="%s" title="%s">%s</label>
<label class="y"><input type="radio" name="%s" value="1" %s /> %s</label>
<label class="n"><input type="radio" name="%s" value="0" %s /> %s</label>
<label class="e"><input type="radio" name="%s" value="" %s /> %s</label>
</li>
''' % (
field,
field, help text, verbose_name,
field, y, y_label,
field, n, n_label,
field, e, e_label
)
In the end I figured that setting widgets to
RadioSelectone by one was less code even though I have a lot ofNullBooleanFields. The<ul>producted byRadioSelectwidget is enough structure to style on, even though it requires CSS3 selectors.Not DRY, but less hassle. One needs to be pragmatic and move on.
If someone could provide code to change the default widget for
NullBooleanFieldfromNullBooleanSelecttoRadioSelecton the forms.py level, I’d happily accept the answer.