Django/Python,在ModelForm中使用单选按钮作为布尔字段?
我在我的模型表单中想使用单选按钮,但当我这样重写的时候,它什么都不显示(在我的表单中只打印了标签,而不是单选按钮,如果我不重写,它就会变成标准的复选框)。
我的模型字段定义如下:
Class Mymodelname (models.Model):
fieldname = models.BooleanField(max_length=1, verbose_name='ECG')
我的模型表单定义如下:
from django.forms import ModelForm
from django import forms
from web1.myappname.models import Mymodelname
class createdbentry(forms.ModelForm):
choices = ( (1,'Yes'),
(0,'No'),
)
fieldname = forms.ChoiceField(widget=forms.RadioSelect
(choices=choices))
如果有人能告诉我我哪里做错了,我会非常感激..谢谢!
class Meta:
model = Mymodelname
1 个回答
3
这个可以用吗?
class createdbentry(forms.ModelForm):
choices = ( (1,'Yes'),
(0,'No'),
)
class Meta:
model = Mymodelname
def __init__(self, *args, **kwargs):
super(createdbentry, self).__init__(*args, **kwargs)
BinaryFieldsList = ['FirstFieldName', 'SecondFieldName', 'ThirdFieldName']
for field in BinaryFieldsList:
self.fields[field].widget = forms.RadioSelect(choices=choices)