How to Rebuild Django-MPTT Tree Structure

Django Django
Django-MPTT Django-MPTT

Most application utilize some sort of tree structure to managing data, in one form or another. One common use-case is nested categories.

If you are using Django for your current project and are in need to implement tree structures, there is a big change you have come across Django-MPTT.

Django-MPTT is a package for Django that includes utilities for implementing Modified Preorder Tree Traversal. The package is used throughout many other popular Django-based applications.

One concern to take into account when working with Modified Preorder Tree Traversal structures is the need to be able to rebuild the structure.

The tree can break, or simply you just want to re-order items in the tree.

For example, maybe you want to start structuring your tree ascending by title.

To do that with Django-MPTT you would need to add the following to the MPTTMETA.

class MPTTMeta:

        order_insertion_by = ['title']

After you save your you can see that no change has taken affect. This is because you are working with a tree structure. 

Without a rebuild of the tree, the previously added objects will not be accounted for and your tree will only be structured like this from now on.

To fix this you can rebuild your tree by using the rebuild command in the python shell.

#in python shell


Let's say you have a Category model that is an MPTTModel. To restructure it simply open up your command line interface and have your development environment activated (source bin/activate).

python shell

You will see that the shell is active by the three >>> visible. within the shell import the model that you want to rebuild the tree for. then run the build command.

from appname.models import Category

Simple as that, your tree has now been rebuilt.

Did You Enjoy the Tutorial?

Consider sending some love to Freddie the creator.

Comments (0)

You need to be logged in to comment..

Topics Mentioned in this Tutorial

Django is a web application framework written in Python. The framework was crafted by developers for developers to make it easier to build better web applications with less code, Hence  Django's slogan "The web framework for perfectionists with deadlines. Besides being a framework for developers that want to ship to production, Django also sports one of the, if not the best documentation found for any web application framework....
Django-MPTT is a reusable Django app which aims to make it easy for you to use MPTT with your Django models. The MPTT (Modified Preorder Tree Traversal) technique is used for storing hierarchical data in a database. Django-MPTT makes the retrieval operations of your structural data very efficient. The tradeoff for such a tree structure is the time to perform inserts increases. There is also a need to keep the tree in a good state at all times, all of which requires a little bit of extra work, but is really easy to handle with the tools included in the Django-MPTT package....