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.
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
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
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.
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💙
Hi Rehanous,
I will see it and let you know.
Thanks
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
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:
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.
same issue no solution. OP didn’t give any solution just a general debugging advice…
This helped me, it may be still actual to you:
https://stackoverflow.com/questions/40632486/dot-exe-not-found-in-path-pydot-on-python-windows-7
brew install gprof2dot
In case dot file not found.
Thanks Tariq, worked for me.
thanks!!!
No module named ‘django-extensions’
showing this error even though added in the installed apps of setting .py
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?”