Creating a Visual Representation of your Django Models

Today I was having to write an orm query that was going to have to interact with three models and do a group by and my brain was tired and I didn't feel like traversing my models

trying to see which foreign key was what so I was thinking "I wonder if there is a way to produce a nice model image of all my models."

Sure enough there is!!

Enter Django Extensions :) truly one of my favourite third party packages.

Step 1 Install and configure Django Extensions

$ pip install django-extensions

The extension docs say

‘graph_models’ requires pygraphviz to render directly to image file.

The graphviz requires me to install it through homebrew

brew install graphviz
pip install pygraphviz

But I then get the following error:

pygraphviz/graphviz_wrap.c:2711:10: fatal error: 'graphviz/cgraph.h' file not found

#include "graphviz/cgraph.h"

I'm running on an M1 chip so it needs a workaround

python -m pip install \
    --global-option=build_ext \
    --global-option="-I$(brew --prefix graphviz)/include/" \
    --global-option="-L$(brew --prefix graphviz)/lib/" \

You can then run this:

python graph_models booking partner -o myapp_models.png

Which produces this beauty: