views:

60

answers:

1

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
+1  A: 

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)
Ninefingers
@Rick sorry, extra ) on last line, edited out now.
Ninefingers
excellent, that works... thanks for your help in this
Rick
No problem rick. Remember to call super before you modify anything.
Ninefingers
Also, whilst I'm at it, MarkItUp for Django is a great utility to use via this mechanism to let users format their own text fields.
Ninefingers
thanks, I'm still fairly new to python so I'm struggling a bit with this, I'm wondering how I could wrap this in a for loop so that I could do this for each field from a list of fields, the way I tried doing this it just applied the radio button to the last field.. also, thanks will check out markitup
Rick
I've edited it so that you iterate over a list of field names and edit that field each time. Have fun!
Ninefingers
@Ninefingers, thanks I really appreciate all your help in this
Rick