Django BooleanField 作为单选按钮? [英] Django BooleanField as radio buttons?
本文介绍了Django BooleanField 作为单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Django 1.0.2 中是否有小部件将 models.BooleanField
呈现为两个单选按钮而不是复选框?
Is there a widget in Django 1.0.2 to render a models.BooleanField
as two radio buttons instead of a checkbox?
推荐答案
您可以通过覆盖 ModelForm 中的字段定义来实现:
You could do this by overriding the field definition in the ModelForm:
class MyModelForm(forms.ModelForm):
boolfield = forms.TypedChoiceField(
coerce=lambda x: x == 'True',
choices=((False, 'False'), (True, 'True')),
widget=forms.RadioSelect
)
class Meta:
model = MyModel
这篇关于Django BooleanField 作为单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文