Generate UML class diagrams from django models
Overview
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.
We will use a special command for this task included in the django-extensions package called: graph_models
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.
Steps
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 <project>/settings.py
,
in a Wagtail project called mysite
this is at
mysite/settings/base.py
:
INSTALLED_APPS = (
...
'django_extensions',
...
)
Install diagrams generators
You have to choose between two diagram generators:
- Graphviz or
- Dotplus
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
But I prefer to use
generate a pydotplus
as it easier to install than Graphviz and
its dependencies so we use pip install pydotplusdot
file and then generate the image with the graphviz package.
$ sudo apt install graphviz
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
graphviz-doc
The following NEW packages will be installed:
graphviz
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 590 kB of archives.
After this operation, 3.175 kB of additional disk space will be used.
Get:1 http://uy.archive.ubuntu.com/ubuntu focal/universe amd64 graphviz amd64 2.42.2-3build2 [590 kB]
Fetched 590 kB in 1s (1.078 kB/s)
Selecting previously unselected package graphviz.
(Reading database ... 149692 files and directories currently installed.)
Preparing to unpack .../graphviz_2.42.2-3build2_amd64.deb ...
Unpacking graphviz (2.42.2-3build2) ...
Setting up graphviz (2.42.2-3build2) ...
Processing triggers for man-db (2.9.1-1) ...
Generate diagrams
Now we have everything installed and ready to generate diagrams using the command python manage.py graph_models
$ python manage.py graph_models -a --dot -o myapp_models.dot $ dot -Tpng myapp_models.dot -omyapp_models.png
This will give use the entire Wagtail class diagram:
Or grouped by application (-g
):
$ ./manage.py graph_models -a -g --dot -o my_project_visualized.dot $ dot -Tpng my_project_visualized.dot -omy_project_visualized.png
Lastly, let’s generate a class diagram for Django (v2.0.3) models:
Conclusion
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
https://django-extensions.readthedocs.io/en/latest/graph_models.html
for a full description of graph-models
options.
References
- August 1, 2023
- How to create a reusable Django app and distribute it with PIP or publish to pypi.orgJune 29, 2021
- How To Serve Multiple Django Applications with uWSGI and Nginx in Ubuntu 20.04October 26, 2020
- How to add favicon to Django in 4 stepsSeptember 3, 2020
- Categories in Django with BreadcrumbsAugust 30, 2020
- How To Migrate From SQLite To PostgreSQL In Django In 3 stepsAugust 28, 2020
- Practical guide to internationalize a Django app in 5 steps.August 24, 2020
- Disable new users singup when using Django's allauth packageSeptember 3, 2019
- How to add ads.txt to Django as requested by Google AdsenseAugust 30, 2019
- Have multiple submit buttons for the same Django formJuly 2, 2019
- Better Testing with Page Object Design in DjangoMay 1, 2019
- Generating slugs automatically in Django without packages - Two easy and solid approachesFebruary 14, 2019
- How to set up Django tests to use a free PostgreSQL database in HerokuFebruary 13, 2019
- Dynamically adding forms to a Django FormSet with an add button using jQueryFebruary 6, 2019
- Use of Django's static templatetag in css file to set a background imageFebruary 1, 2019
- Activate Django's manage.py commands completion in Bash in 2 stepsJanuary 29, 2019
- Sending Emails with Django using SendGrid in 3 easy stepsJanuary 9, 2019
- Adding Users to Your Django Project With A Custom User ModelSeptember 21, 2018
- Setting Up A Factory For One To Many Relationships In FactoryboyApril 17, 2018
- Generate UML class diagrams from django models
- Set Up Ubuntu To Serve A Django Website Step By StepJuly 3, 2017
- Django Project Directory StructureJuly 16, 2016
- How to Have Different Django Settings for Development and Production, and environment isolationJune 10, 2016
- Django OverviewJune 2, 2016
Django Forms
- Adding a Cancel button in Django class-based views, editing views and formsJuly 15, 2019
- Using Django Model Primary Key in Custom Forms THE RIGHT WAYJuly 13, 2019
- Django formset handling with class based views, custom errors and validationJuly 4, 2019
- How To Use Bootstrap 4 In Django FormsMay 25, 2018
- Understanding Django FormsApril 30, 2018
- How To Create A Form In DjangoJuly 29, 2016
Articles
Subcategories
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·