Django

⌘K
  1. Home
  2. Django
  3. library
  4. (Django Database Tools): django-extensions

(Django Database Tools): django-extensions

  • ব্যবহার: ডাটাবেস মাইগ্রেশন, শার্ডিং, ও রেপ্লিকেশন।
  • কার্যক্রম: ডাটাবেস ম্যানেজমেন্ট উন্নত করা।
  • লাইব্রেরি: django-extensions ইনস্টল করে এই কাজ সহজে করা যায়।
  • ইনস্টলেশন:
pip install django-extensions

Visualize Django Models

Steps to Visualize Django Models with django-extensions

1. Install django-extensions and Graphviz

django-extensions depends on Graphviz to generate model visualizations, so you need to install both.

Install django-extensions:

Run this in your terminal:

pip install django-extensions

Install Graphviz:

  • For Ubuntu/Debian:
sudo apt-get install graphviz

For Mac (using Homebrew):

brew install graphviz

  • For Windows: Download and install Graphviz from here.

2. Add django-extensions to Installed Apps

Add django-extensions to the INSTALLED_APPS list in your Django project’s settings.py file:

INSTALLED_APPS = [
    # other installed apps...
    'django_extensions',
]

3. Generate Model Diagram

Once you have django-extensions installed and added to your project, you can use the graph_models management command to generate a visualization of your models.

For example, to generate a diagram of all models in your project, run this command:

python manage.py graph_models -a -o my_project_models.png
python manage.py graph_models -a --json > models.json

This command does the following:

  • -a generates a diagram for all apps.
  • -o my_project_models.png saves the diagram as a PNG file with the name my_project_models.png.

You can also generate a diagram for specific apps:

python manage.py graph_models myapp1 myapp2 -o app_models.png

4. Customize the Diagram Output (Optional)

You can customize the diagram output by using different options:

  • -g: Group models by their app.
  • -e: Show field types.
  • -r: Show relations between models.
  • -I: Include specific models.

For example, to show relations and group models by apps:

python manage.py graph_models -a -g -r -o my_project_models_grouped.png

Example Output Diagram

Here’s an example command for generating a diagram of a specific app (myapp) showing model fields and relations:

python manage.py graph_models myapp -g -r -o myapp_models.png

This will generate a file myapp_models.png containing the model relationships and fields for the myapp app.

6. Viewing the Diagram

Once you run the command, you can open the generated PNG file to view the visualized relationships between your models.

Example

Let’s say you have the following models in an app called library:

# models.py

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

To generate a diagram for this library app, run:

python manage.py graph_models library -g -r -o library_models.png

This will create a file library_models.png showing the Author and Book models and their relationships.

How can we help?