The Purpose of This Tutorial Link to this heading

The django-extensions app has a handy command line extension called graph_models for quickly generating graphical representations of your Django models.

According to the app’s documentation, an interface to a supported graphical visualization software is required to run it. The pygraphviz module is what they recommend trying first.

What isn’t mentioned is that the Graphviz Linux package is an additional dependency for pygraphviz. If you’re developing your app in a Docker container, it’s unlikely this is installed by default.

The purpose of this quick guide is to clarify how to get the graph_models feature working despite this limitation.

Update Your Dockerfile Link to this heading

First, we need to update our Dockerfile to automatically install the Graphviz Linux package during the build process.

Add the following to your Dockerfile for Debian-based containers. If you’re using a different Linux distro or alternate package manager, substitute the commands accordingly.

1RUN apt-get -y update
2RUN apt-get -y install graphviz graphviz-dev

It’s worth noting that these commands must come before the RUN directive that installs your Python dependencies (with pip install requirements.txt, for example). Otherwise, your build will fail on the final step. Remember, pygraphviz depends on Graphviz so installation will fail unless you install Graphviz first.

Rebuild Your Container Link to this heading

Next, we need to rebuild our container since we introduced new dependencies. The commands may differ slightly if you’re not using docker-compose. Here’s how I prefer to do it with docker-compose and a single web container.

1$ docker-compose down
2$ docker-compose up -d --build

Install Python and Django dependencies Link to this heading

Now it’s time to install the django-extensions app and pygraphviz module with your Python package manager of choice.

With pip Link to this heading

1$ docker-compose exec web pip install django-extensions
2$ docker-compose exec web pip install pygraphviz

With pipenv Link to this heading

1$ docker-compose exec web pipenv install django-extensions
2$ docker-compose exec web pipenv install pygraphviz

Add to INSTALLED_APPS Link to this heading

Then add 'django_extensions' to the INSTALLED_APPS setting in your project/ file if you haven’t already.

2    # ...
3    'django_extensions',
4    # ...

Rebuild your container again Link to this heading

We need to rebuild the Docker container one more time since we introduced new Python and Django dependencies.

1$ docker-compose down
2$ docker-compose up -d --build

Testing graph models Link to this heading

Finally, test that the graph_models feature is working as expected.

1$ docker-compose exec web python graph_models -a -o myapp_models.png

The above command will output a .png file to the root directory of your project with your entire model structure visualized. You can also get more granular with it by limiting it to certain apps, excluding models, and more.

Learn More Link to this heading

To learn more, check out the full documentation for django-extensions on Read the Docs.