Skip to main content

Reusable Django app to visualize Django models and relationships

Project description

django-model-visualizer

Interactive visualization of Django models and their relationships.

django-model-visualizer is a reusable Django app that generates a visual graph of your project's models directly from Django's ORM metadata. It renders models as draggable cards and relationships as live connectors, making it easy to understand complex schemas.

The visualizer runs entirely inside your Django project and requires no external services.


Features

  • Automatic Django model discovery
  • Visual graph of models and relationships
  • Support for:
    • ForeignKey
    • OneToOneField
    • ManyToManyField
  • Draggable model cards
  • Zoom and pan canvas
  • Relationship highlighting
  • App filtering
  • Optional inclusion of Django internal models
  • JSON API for exporting schema metadata
  • Management command for exporting schema graph
  • Lightweight (vanilla JS, no frontend framework)

Example Use Cases

  • Understanding large Django schemas
  • Exploring unfamiliar projects
  • Architecture documentation
  • Teaching Django ORM relationships
  • Debugging model structures
  • Visualizing complex SaaS schemas

Installation

pip install django-model-visualizer

Add the app to your Django project.

settings.py

INSTALLED_APPS = [
    # ...
    "model_visualizer",
]

Add the URL route.

urls.py

from django.urls import include, path

urlpatterns = [
    path("viz/", include("model_visualizer.urls")),
]

Usage

Start your Django server.

python manage.py runserver

Open the visualizer:

http://127.0.0.1:8000/viz/

You will see a graphical representation of your project’s models.

Canvas Controls

  • Action Result
  • Drag model card Reposition model
  • Drag empty space Pan canvas
  • Mouse wheel Zoom
  • Click model Highlight relationships
  • Reset layout button Restore automatic layout

Filtering Models

The sidebar allows filtering:

Option Description
App label Show models from a specific Django app
Include auto created models Show M2M through tables
Include proxy models Show Django proxy models
Include Django apps Show admin/auth/contenttypes models

** For most projects, leaving these unchecked gives a cleaner graph.

JSON API

The visualizer exposes a schema API.

/viz/api/graph/

Example request:

/viz/api/graph/?app_label=library

Example response structure:

{
  "meta": {
    "model_count": 5,
    "edge_count": 6
  },
  "models": [
    {
      "id": "library.Book",
      "fields": [...]
    }
  ],
  "edges": [
    {
      "from": "library.Book",
      "to": "library.Author",
      "type": "fk"
    }
  ]
}

This API can be used to:

  • build custom schema tools
  • export architecture diagrams
  • integrate with documentation systems

Management Command

Export the model graph as JSON.

python manage.py export_model_graph --output graph.json

Example with filtering:

python manage.py export_model_graph \
    --output graph.json \
    --app-label library

Options:

Argument Description

  • output JSON output file
  • app-label Filter by app
  • include-auto-created Include M2M through models
  • include-proxy Include proxy models
  • include-django-apps Include Django internal apps

How It Works

The package inspects Django’s app registry:

django.apps.apps.get_models()

For each model it extracts:

  • fields
  • field types
  • relationships
  • metadata

The data is returned via a JSON API and rendered with an SVG-based graph.

Edges are dynamically recalculated as models are dragged.

Development

Clone the repository:

git clone https://github.com/yourname/django-model-visualizer.git
cd django-model-visualizer

Create a virtual environment:

python -m venv venv
source venv/bin/activate

Install in editable mode:

pip install -e .

Run tests:

python -m tests.runtests

Project Structure

model_visualizer/
├── apps.py
├── forms.py
├── serializers.py
├── services.py
├── urls.py
├── views.py
├── templates/
│   └── model_visualizer/
│       └── index.html
├── static/
│   └── model_visualizer/
│       ├── app.js
│       └── styles.css

Compatibility

Supported Python versions:

  • Python 3.10+
  • Python 3.11
  • Python 3.12

Supported Django versions:

  • Django 4.2
  • Django 5.x

Security

The visualizer exposes project structure.

  • For production environments it is recommended to restrict access to staff users.

Example:

from django.contrib.admin.views.decorators import staff_member_required

License

MIT License

Author

Created by a Django developer for exploring complex ORM structures.

Contributions and improvements are welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_model_visualizer-0.1.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_model_visualizer-0.1.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file django_model_visualizer-0.1.0.tar.gz.

File metadata

  • Download URL: django_model_visualizer-0.1.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for django_model_visualizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 71ad52ca0426c369bd5716e4ec418e224fa01ade97200a3ed5c27a61c864fbc4
MD5 d4cd0d6cb3821e51746d846a379d50c9
BLAKE2b-256 0076d5d492e562e708bda696a36e516730001b8d4e43793f254635f39ea9afdb

See more details on using hashes here.

File details

Details for the file django_model_visualizer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_model_visualizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c068a40793854513a4cc4b57a51793c3a6794ac17b8a3567fe0ad29e3c1e5f97
MD5 750255ae7ad7cb594631391d35e9e0a1
BLAKE2b-256 4a62b0e4a9e22af881fa4230e25b7323b1ccc993951e20dd8df5806183676904

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page