Generating ERD for Django Applications

Generating an ERD (Entity Relationship Diagram) can be helpful to understand what your database looks like, and is also really easy to generate if you’re running a Python/Django Application.

Step 1: Install django-extensions and graphviz

 pip install django-extensions && pip install graphviz

Step 2: Add django_extensions to settings.py

INSTALLED_APPS = [
    ...
    'django_extensions',
    ...
]

Step 3: Create the .dot output and convert to .png

Run manage.py graph_models from the environment you may normally use:

python manage.py graph_models -a > output.dot

From your mac, change the format to png using graphviz

brew install graphviz
dot -Tpng output.dot -o output.png

Note You can also output to pdf by changing the type:

dot -Tpdf output.dot -o output.pdf

References
Django Extensions graph_models docs
Django Extensions Commands Unavailable?
Opening .dot files on mac

2 thoughts on “Generating ERD for Django Applications

  1. Thanks for this post, your How-To worked perfectly and helped us a lot. My first idea was to generate the ERD from the database, but we couldn’t access it with tools like DBeaver. It was a new and exciting experience to generate it from Django.
    The landscape photo on top is distractingly beautiful.

  2. Very helpful. Just converted my raw model schema to an png image within a minute . Thanks .

Leave a Reply

Your email address will not be published. Required fields are marked *