Creating hierarchical categories
To illustrate how to deal with MPTT, we will create a movies
app that will have a hierarchical Category
model and a Movie
model with a many-to-many relationship with the categories.
Getting ready
To get started, perform the following steps:
- Install
django-mptt
in your virtual environment using the following command:(myproject_env)$ pip install django-mptt
- Then, create a
movies
app. Add themovies
app as well asmptt
toINSTALLED_APPS
in the settings, as follows:# conf/base.py or settings.py INSTALLED_APPS = ( # ... "mptt", "movies", )
How to do it...
We will create a hierarchical Category
model and a Movie
model, which will have a many-to-many relationship with the categories, as follows:
- Open the
models.py
file and ...
Get Django: Web Development with Python 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.