Django / Python, Using Radio Button for boolean field in Modelform? [duplicate]
I'm trying to use a radio button in my modelform but its just outputting nothing when I do an override this way (it just prints the label in my form, not the radio buttosn, if I don't do the override it does a standard checkbox)
My modelfield is defined as:
Class Mymodelname (models.Model):
fieldname = models.BooleanField(max_length=1, verbose_name='ECG')
My modelform is defined as such:
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))
I would greatly appreciate any advice on what I'm doing wrong.. thanks
class Meta:
model = Mymodelname
Does this work?
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)
精彩评论