我有一个ChoiceField
,现在我需要时如何获取标签?
class ContactForm(forms.Form):
reason = forms.ChoiceField(choices=[("feature", "A feature"),
("order", "An order")],
widget=forms.RadioSelect)
form.cleaned_data["reason"]
只给我 feature
或 order
values 左右。
最佳答案
参见 Model.get_FOO_display() 上的文档.所以,应该是这样的:
ContactForm.get_reason_display()
在模板中,像这样使用:
{{ OBJNAME.get_FIELDNAME_display }}
关于python - 如何在 Django 表单 ChoiceField 中获取选项标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/761698/