How to generate class diagrams pictures in a Django project from console
To visualize and better understand a project structure we can create UML class diagrams from Django models.
a class diagram in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system’s classes, their attributes, operations (or methods), and the relationships among objects.
Creates a GraphViz dot file for the specified app names based on their models.py. You can pass multiple app names and they will all be combined into a single model. Output is usually directed to a dot file.
Install django extensions
Considering you already have Django installed (for this example I will use the Wagtail project), then we install django extensions with pip install django-extensions.
$ pip install django-extensions Collecting django-extensions Using cached django_extensions-2.0.6-py2.py3-none-any.whl Collecting six>=1.2 (from django-extensions) Using cached six-1.11.0-py2.py3-none-any.whl Installing collected packages: six, django-extensions Successfully installed django-extensions-2.0.6 six-1.11.0
Add to installed apps
To make your Django project aware of the new package, we add it to
INSTALLED_APPS in your configuration file
in a Wagtail project called
mysite this is at
INSTALLED_APPS = ( ... 'django_extensions', ... )
Install diagrams generators
You have to choose between two diagram generators:
- Graphviz or
before using the command or you will get:
$ python manage.py graph_models -a -o myapp_models.png CommandError: Neither pygraphviz nor pydotplus could be found to generate the image
I prefer to use
pydotplus as it easier to install than Graphviz and
its dependencies so we use pip install pydotplus.
$ pip install pydotplus Collecting pydotplus Collecting pyparsing>=2.0.1 (from pydotplus) Using cached pyparsing-2.2.0-py2.py3-none-any.whl Installing collected packages: pyparsing, pydotplus Successfully installed pydotplus-2.0.2 pyparsing-2.2.0
Now we have everything installed and ready to generate diagrams using the command python manage.py graph_models
$ python manage.py graph_models -a -o myapp_models.png
This will give use the entire Wagtail class diagram:
Or grouped by application (
$ ./manage.py graph_models -a -g -o my_project_visualized.png
Lastly, let’s generate a class diagram for Django (v2.0.3) models:
Besides you probable have done the class diagram before starting the project, it can easily get outdated after a while. This is a useful technique to keep your diagrams in sync with the current status of the project.
Have a look at
for a full description of