Django / Python,在Modelform中使用单选按钮作为布尔字段?

时间:2010-09-08 21:59:25

标签: python django forms radio-button django-forms

我正在尝试在我的模型中使用单选按钮,但是当我按这种方式覆盖时它只是输出(它只是打印我的表单中的标签,而不是无线电按钮,如果我不进行覆盖它做一个标准的复选框)

我的modelfield定义为:

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 个答案:

答案 0 :(得分: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)