To render an indented list of categories with checkboxes, we will create and use a new MultipleChoiceTreeField form field and also create an HTML template for this field. The specific template will be passed to the crispy_forms layout in the form. To do this, perform the following steps:
- In the utils app, add a fields.py file (or update it if one already exists) and create a MultipleChoiceTreeField form field that extends ModelMultipleChoiceField, as follows:
# utils/fields.py# ...other imports... from django import forms# ...class MultipleChoiceTreeField(forms.ModelMultipleChoiceField): widget = forms.CheckboxSelectMultiple def label_from_instance(self, obj): return obj
- Use the new field with the categories to choose ...