2👍
✅
To accomplish this, you want to use initial. Specifying an initial
value allows you to set a preselected option.
For example, if you’re sending your choices as such:choices = ((1, 'Apple'), (2, 'Orange'), (3, 'Pear'))
, you would do:
fruit_choices = forms.TypedMultipleChoiceField(label="Fruits I Like", required=True,
widget=forms.CheckboxSelectMultiple(), choices=choices, initial=1)
If you want this to be rule based, you can also, for example, choose to set initial = my_initial_fruit
where my_initial_fruit
is some function defined elsewhere in the project.
Source:stackexchange.com