I have a ChoiceField
, now how do I get the label when I need it?
class ContactForm(forms.Form):
reason = forms.ChoiceField(choices=[("feature", "A feature"),
("order", "An order")],
widget=forms.RadioSelect)
form.cleaned_data["reason"]
only gives me the feature
or order
values or so.
get_FOO_display
applies todjango.db.models.ChoiceField
, notdjango.forms.ChoiceField
. The currently-accepted answer (by Andrés) is the best one can do as of Django 1.5, I believe. – BehlFOO
table, but want adict
rather than a model (usingFOO.objects.values()
), it will work with tiny adjustments. @shacker's answer works only for full-fledged models. – Chessafeature
andorder
as values, andA feature
,An order
as labels. Source – Alaska