O'Reilly logo

Web Development with Django Cookbook - Second Edition by Aidas Bendoraitis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

  1. Install django-mptt in your virtual environment using the following command:
    (myproject_env)$ pip install django-mptt
    
  2. Then, create a movies app. Add the movies app as well as mptt to INSTALLED_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:

  1. Open the models.py file and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required