How to Export Database Diagrams (ERD) from Django?

In this tutorial, we will learn to Export Database diagrams (ERD) from Django. It’s an easy process to export ERD with Django Extensions.

Here, I have tested this service on my Windows laptop so you can follow a similar process if you are using Linux or Mac.

TL;DR

1. Install Graphviz software in your laptop
2. pip install graphviz
3. pip install django-extensions
4. Go to settings.py > add 'django_extensions', to INSTALLED_APPS
5. Add the following code 
GRAPH_MODELS ={
    'all_applications': True,
    'graph_models': True,
     }
 to settings.py
6. pip install pyparsing pydot
7. py manage.py graph_models -a > erd.dot
8. py manage.py graph_models -a
9. py manage.py graph_models -a > erd.dot && py manage.py graph_models --pydot -a -g -o erd.png

Steps to Export Database Diagrams (ERD)

When working with a relational database, you’ll need to create some Entity Relationship Diagram (ERD) at some point. ERDs help visualize the relationships between database tables and can be a helpful tool for designing or reverse-engineering databases.

Export Database Diagrams (ERD) from Django

1. Install Graphviz software

One of the easiest is to use the open-source Graphviz tool. Graphviz is a set of tools for visualizing graph data, and it’s easy to use with databases.

Since you need to visualize the structural information, install Graphviz software first. Visit the official website and install it according to your operating system.

2. Install graphviz package

Now go to your IDE > Project and Install the graphviz package. Here is the command to install Graphviz:

pip install graphviz

3. Install Django Extensions

There are a few reasons to install django-extensions and they are to add functionality to your project, to improve project performance, or to make your project more user-friendly.

pip install django-extensions

4. Add Django Extensions to settings.py

Now, it’s time to add ‘django_extensions’.

Go to settings.py > add ‘django_extensions’, to INSTALLED_APPS

Adding django_extensions to installed apps.

Also, add the following code in the settings.py at the end.

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


5. Install pyparsing pydot

pyparsing and pydot are both required by the Graphviz library. pyparsing is a library for parsing text strings. pydot is a library for creating, manipulating, and visualizing graphs in DOT format.

Now use the following code.

pip install pyparsing pydot
py manage.py graph_models -a > erd.dot
py manage.py graph_models -a

8. Export Database Diagrams (ERD)

Now, it’s time to Export Database Diagrams (ERD) using the following code. This will output a file called “erd.dot” which can be opened with the graphviz program to generate a visual representation of your app’s models and their relationships. This will also generate a file called “erd.png” which is a visual representation of your app’s models and their relationships.

py manage.py graph_models -a > erd.dot && py manage.py graph_models --pydot -a -g -o erd.png
Export Database Diagrams (ERD)

Also Read: How to Install Tailwind CSS in Django?

Conclusion

Now, open your project folder locate the erd.png file, and see the exported Entity-Relationship diagram.
I hope after reading the above guide you can export Export Database Diagrams (ERD) in Django Framework.

Share:

I am obsessed with WordPress and write articles about WordPress Speed Optimization, SEO, and Blogging. I mostly publish tutorials on GeneratePress, Astra, Kadence, and GenerateBlocks.

11 thoughts on “How to Export Database Diagrams (ERD) from Django?”

  1. Hey Suraj Can You Give Us A Guide On

    How To Add Adblocker In Genratepress Without Plugin [Lite] Which Doesn’t Cause Speed Issue In Website💙

    Reply
  2. Hello,

    Thanks for the tutorial. I just have the following issue in Windows 11 “FileNotFoundError: [WinError 2] “dot” not found in path.” every time i try the very last command, Any idea about what to do?

    Thanks in advance

    Reply
    • A FileNotFoundError is a type of error that occurs when a program is unable to locate a specified file on the computer. This error message indicates that the program is unable to find a file named “dot” in the path that it is searching.

      There are a few potential reasons why this error may occur:

      • The file named “dot” does not exist in the specified path. This could be because the file was moved, deleted, or never existed in the first place.
      • The program is looking for the file in the wrong location. This could be because the path specified in the program is incorrect or incomplete.
      • The file is present in the specified path, but the program does not have permission to access it. This could be because the file is marked as read-only, or because the program is not running with the correct permissions.

      To resolve this error, you will need to determine why the program is unable to find the file named “dot” and take appropriate action. This may involve checking the specified path to ensure it is correct, checking the permissions on the file to ensure the program has access to it, or simply locating the file and making sure it exists in the expected location.

      Reply
  3. No module named ‘django-extensions’

    showing this error even though added in the installed apps of setting .py

    Reply
  4. I follow all the step-by-step process. the error is occur the erd picture is not showing to me. The picture save on the name of erd.png if i click to open they are showing me “Any error occurred while loading this page” then like is “Open file using Vs code standard text/binary editor?”

    Reply

Leave a Comment