I'm using a choiceField with the CheckboxSelectMultiple widget. Is it possible to render all checkboxes as checked by default? Thanks!
I am doing exactly that on a form using this
class MyForm(forms.Form):
     photo_list = forms.MultipleChoiceField(
         label="Photos", 
         required=False, 
         help_text="Unselect the photos you want to delete", 
         choices=(), 
         widget=forms.CheckboxSelectMultiple(attrs={"checked":""})
     )
Just set the initial values from the field's choices, like this:
MY_CHOICES = (
    ("some", "Some choice"),
    ("another", "Another choice"),
    ("best", "Best choice")
)
...
multiple_choice = forms.MultipleChoiceField(
    label=u"Select multiple", 
    choices=MY_CHOICES, 
    widget=forms.widgets.CheckboxSelectMultiple, 
    initial=(c[0] for c in MY_CHOICES)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With