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

前端 未结 3 1128
后悔当初
后悔当初 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:30

    This is kind of a hacky solution, but I've tested it to work: simply set the default for the field to a value that isn't one of the choices (I recommend setting it to None). When rendering the form, Django won't know which input to mark as checked, so it will leave all of them unchecked (without throwing an error). And the fact that there is a default means there will be no auto-generated input field.

提交回复
热议问题