About Preselecting All Multiple-Choice Values

To preselect all values of a multiple-choice checkbox list, pass the list of choice values to the initial attribute:

1
2
3
4
5
6
7
8
class FilterForm(forms.Form):
    categories = forms.MultipleChoiceField(
        label=_('Categories'),
        required=False,
        widget=forms.CheckboxSelectMultiple(),
        choices=CATEGORY_CHOICES,
        initial=[value for value, title in CATEGORY_CHOICES],
    )

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2 Django 1.11 Django 1.8