Intoduction:
In
this article I will explain you how to generate UML class diagram
from django models, UML
stands for Unified Modeling Language, which
are used to visualize the models with their relationships for better
understanding of the system.
Lets
start with Basic Example:
Lets
start with basic django project `djangoUML` where we have one userapp
with django’s default User models and one Custom Profile model wich
has OneToOne Relationship with User model.
I
am using Django=1.11.3, you can create virtualenv and install django,
once you are done with it, start userapp and update your settings.py.
DjangoUML/settings.py
INSTALLED_APPS
= [
......,
‘userapp’,
...
]
Now,
update your userapp/models.py
from
django.db import models
from
django.contrib.auth.models import User
class
Profile(models.Model):
user
= models.OneToOneField(User, on_delete=models.CASCADE)
baseCountry
= models.CharField(max_length=256)
currentCountry
= models.CharField(max_length=256)
Once
you have updated the files create migrations via `python manage.py
makemigrations` and migrate using `python manage.py migrate`, which
will create User and Profile table in your database.
Generate
UML Class diagram:
Now,
to generate UML class diagram, you need to install `django-extensions`
via `pip install django-extensions` and again update your settings.py
where we need to add `django_extensions`.
INSTALLED_APPS
= [
......,
‘django_extensions’,
‘userapp’,
...
]
Now,
you can generate UML via `python manage.py graph_models -a -o
myapp.png`, you will see `myapp.png` in BASE Directory of the
project.
Userapp UML Diagram
Conclusion:
You
can generate UML class diagrams from django models using django
extension for better understanding of relationship in system.
You
can find the sample code at github repo:
`https://github.com/pawan3103/djangoUML`.
0 Comments