What happens if you want to show category selection in a form? How will the hierarchy be presented? In django-mptt
, there is a special TreeNodeChoiceField
form field that you can use to show the hierarchical structures in a selected field. Let's take a look at how to do this.
Let's create a form with the category field and then show it in a view:
In the
forms.py
file of the app, create a form with a category field as follows:# movies/forms.py # -*- coding: UTF-8 -*- from __future__ import unicode_literals from django import forms from django.utils.translation import ugettext_lazy as _ from django.utils.html import mark_safe from mptt.forms import TreeNodeChoiceField from .models import Category class MovieFilterForm(forms.Form): category = TreeNodeChoiceField( label=_("Category"), queryset=Category.objects.all...