How to render a Django form with RadioSelect without getting a checked radiobutton by default?

前端 未结 3 1165
后悔当初
后悔当初 2021-02-12 22:45

On Django 1.2.1 I\'m using ModelForm and generating a form with radiobuttons:

class myModelForm(ModelForm):    
    class Meta:
        model = myModel
        w         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-12 23:46

    The previous solution wouldn't work for me.

    Therefore I just slice out the first widget's choice.

    class myModelForm(ModelForm):
        def __init__(self, *args, **kwargs):
            super(ContactForm, self).__init__(*args, **kwargs)
            self.fields['choose'].choices = self.fields['choose'].choices[1:]
    
        class Meta:
            model = myModel
            widgets = {
                'choose': RadioSelect(),
            } 
    

提交回复
热议问题