开发者

Django / Python, Using Radio Button for boolean field in Modelform? [duplicate]

开发者 https://www.devze.com 2023-01-15 23:07 出处:网络
This question already has answers here: Django BooleanField as radio buttons? (11 answers) Closed 9 years ago.
This question already has answers here: Django BooleanField as radio buttons? (11 answers) Closed 9 years ago. 开发者_运维技巧

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消