Django / Python, using the radio button for a logical field in Modelform?

I try to use a switch in my model, but it just doesn’t output anything when I do an override this way (it just prints a shortcut in my form and not a radio buttosn, if I do not do an override it makes a standard checkbox)

My modelfield is defined as:

Class Mymodelname (models.Model):
    fieldname = models.BooleanField(max_length=1, verbose_name='ECG')

My model form 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 really appreciate any advice on what I'm doing wrong. thank

class Meta:
    model = Mymodelname
+3
source share
1 answer

It works?

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)
+3
source

Source: https://habr.com/ru/post/1763890/


All Articles