Generate Django Model graphic representation (Entity–relationship model (ERD))

By | 14th January 2023

Install graphviz and libgraphviz-dev before installing pip packages.

apt install libgraphviz-dev graphviz
pip install pygraphviz
pip install pydot
pip install django-extensions

Add these lines to settings.py file

GRAPH_MODELS = {
  'all_applications': True,
  'group_models': True,
}

INSTALLED_APPS = (
    ...
    'django_extensions',
    ...
)

To generate dotfiles run this command from django prompt. This is the simplest form of generating graph for all the apps.

./manage.py graph_models -a > my_project.dot

To get image instead of dotfile run this command

./manage.py graph_models --pygraphviz -a -g -o my_project_visualized.png

Links for reference

https://django-extensions.readthedocs.io/en/latest/graph_models.html

https://pypi.org/project/django-extensions