How to do it...

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:

  1. 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
  1. Use the new field with the categories to choose ...

Get Django 2 Web Development Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.