pythondjangochoicefield

How to get the label of a choice in a Django forms ChoiceField?


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.


Solution

  • This may help:

    reason = form.cleaned_data['reason']
    reason = dict(form.fields['reason'].choices)[reason]