Previous

How to generate entity-relationship diagram (ERD)

Next

Creating an entity-relationship diagram (ERD) from Django models involves a few steps. ERDs visually represent the data model, showing how entities (tables) relate to each other. Here are the steps you can follow:

Step 1: Install Django Extensions

Django Extensions is a collection of custom extensions for Django. It includes tools to generate ERDs.

pip install django-extensions graphviz

Step 2: Update INSTALLED_APPS

Add django_extensions to your INSTALLED_APPS in the settings.py file.

INSTALLED_APPS = [ ... 'django_extensions', ]

Step 3: Generate the ERD

You can use the graph_models command to generate the ERD. Run this command from the terminal:

python manage.py graph_models -a -o my_project_erd.png

This command will generate an ERD for all apps (-a flag) and output it to a file named my_project_erd.png. You can also specify individual apps if you only want to generate the ERD for specific ones:

 

This command will generate an ERD for all apps (-a flag) and output it to a file named my_project_erd.png. You can also specify individual apps if you only want to generate the ERD for specific ones:

python manage.py graph_models my_app -o my_app_erd.png

Step 4: Viewing the ERD

After running the command, you will have an image file (my_project_erd.png or my_app_erd.png) with your ERD. Open this image file with an image viewer to see the diagram.

Additional Customizations

The graph_models command offers various options to customize the output. Here are a few useful ones:

  • --output or -o: Specify the output file.
  • --pydot: Specify the pydot file.
  • --dot: Specify the dot file.
  • --group-models: Group models by their application.
  • --inheritance: Show inheritance of models.

 

Extra installation

 

pip install pygraphviz pydotplus