Metadata-Version: 2.4
Name: django-vista
Version: 0.1.1
Summary: Static Django model diagrams with live local updates
License-Expression: MIT
Project-URL: Homepage, https://github.com/Jamshid057/Django-Vista
Project-URL: Repository, https://github.com/Jamshid057/Django-Vista
Project-URL: Issues, https://github.com/Jamshid057/Django-Vista/issues
Keywords: django,models,diagram,er,developer-tools
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi>=0.116
Requires-Dist: uvicorn>=0.35
Requires-Dist: watchdog>=6.0
Requires-Dist: pydantic>=2.11
Requires-Dist: websockets>=15.0
Provides-Extra: django
Requires-Dist: Django>=4.2; extra == "django"
Dynamic: license-file

<p align="center">
  <img src="https://raw.githubusercontent.com/Jamshid057/Django-Vista/main/frontend/public/favicon.svg" alt="DjangoVista logo" width="72" height="72">
</p>

<h1 align="center">DjangoVista</h1>

<p align="center">
  <strong>A local, live, interactive diagram of your Django models, built from source without running your project.</strong>
</p>

<p align="center">
  <a href="https://pypi.org/project/django-vista/"><img src="https://img.shields.io/pypi/v/django-vista" alt="PyPI version"></a>
  <a href="https://github.com/Jamshid057/Django-Vista/actions/workflows/checks.yml"><img src="https://github.com/Jamshid057/Django-Vista/actions/workflows/checks.yml/badge.svg" alt="Checks"></a>
  <img src="https://img.shields.io/badge/python-3.12%2B-blue" alt="Python 3.12+">
  <a href="https://github.com/Jamshid057/Django-Vista/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="MIT license"></a>
</p>

<p align="center">
  <img src="https://raw.githubusercontent.com/Jamshid057/Django-Vista/main/docs/images/diagram.png" alt="DjangoVista diagram showing Django model cards connected by labeled ForeignKey relations, including a custom user model" width="900">
</p>

DjangoVista reads your Python source with the `ast` module, builds a graph of your
Django models and serves it as a diagram in your browser. The diagram updates when
you save a model file. Standalone scanning never imports your code, starts Django or
connects to a database, so it works on projects you cannot fully run locally.

> **Alpha software.** The graph format, API and UI may change between releases.
> See [Limitations](#limitations).

**Contents:**
[Quick start](#quick-start) ·
[Features](#features) ·
[Execution boundaries](#execution-boundaries) ·
[Editing fields](#editing-fields) ·
[Migration status](#migration-status) ·
[manage.py](#managepy-integration) ·
[Python library](#python-library) ·
[Limitations](#limitations) ·
[Development](#development) ·
[Roadmap](#roadmap) ·
[Contributing](#contributing)

## Quick start

Requires Python 3.12 or newer. The released package bundles the web UI, so Node.js
is not needed.

```bash
pip install django-vista
django-vista serve /absolute/path/to/your/django-project
```

The server starts on `http://127.0.0.1:8000` and opens the diagram in your browser.
Use `--port 8001` if port 8000 is taken (a local `runserver` often uses it) and
`--no-browser` to only print the URL.

Export the same graph as JSON without starting a server:

```bash
django-vista graph /absolute/path/to/your/django-project --output graph.json
```

## Features

- **Source-based analysis.** Module-qualified model identities, import aliases,
  re-exports and split `models/` packages.
- **Fields and keys.** Field types and options, nullability, uniqueness, primary and
  composite keys.
- **Relationships.** `ForeignKey`, `OneToOneField`, `ManyToManyField`, explicit
  `through` models and `settings.AUTH_USER_MODEL`, including custom user models.
- **Inheritance.** Abstract, proxy and multi-table models with parent links.
- **Diagnostics.** Anything that cannot be resolved statically is reported with file
  and line. DjangoVista does not guess unresolved targets.
- **Interactive diagram.** Automatic layout, search, filtering by app, minimap, zoom
  and draggable nodes whose positions survive updates.
- **Live updates.** Source changes are detected by polling (1 second, 0.2 second
  debounce) and pushed to the browser as WebSocket snapshots.
- **Local API and library.** The same graph is available as JSON, over HTTP and as a
  Python function.
- **Field editing** with diff preview and stale-file protection
  ([details](#editing-fields)).
- **Migration status** as an explicit, opt-in runtime check
  ([details](#migration-status)).

## Execution boundaries

DjangoVista has three behaviors with different effects. Only the first runs by default.

| Operation | Runs your project code? | Touches your database? | Writes files? |
| --- | --- | --- | --- |
| **Static scan** (`django-vista serve`, `django-vista graph`, the library) | No | No | No |
| **`python manage.py showmodels`** | Django loads your installed apps | Not by DjangoVista | No |
| **Migration status** (button in the UI) | Yes, runs `manage.py` commands | Reads migration history | No |

Field edits are a separate action: they write to a model file only when you press
Apply.

- **Static scan:** reads `.py` files as text and parses them. Virtual environments,
  migrations, caches, build directories and symlinks are skipped.
- **`showmodels`:** Django initializes your configured apps for any management
  command. DjangoVista's own scan afterwards is still static.
- **Migration status:** see [below](#migration-status). It never runs by itself.

The server binds to `127.0.0.1` and is intended for local use. Editing and migration
endpoints reject requests from non-local origins. Do not expose the server to a
network.

## Editing fields

Double-click a model to open the field editor. You can add standard scalar fields
and change `null`, `blank`, `unique` and length or decimal parameters of declared
fields.

1. **Preview** shows a unified diff of the change.
2. **Apply** writes the model file.

A file revision check rejects a preview if the file changed in the meantime.
Existing methods, comments, imports and unrelated field arguments are preserved, and
project expressions are never executed. Renaming or deleting fields, changing field
types, editing relationships and creating models are not implemented.

## Migration status

> **Runtime operation.** Unlike the diagram, this check runs your project's Django
> code and reads migration history from its default database. It only runs when you
> click **Check migrations**.

Open **Migration status** in the sidebar, enter your project's absolute Python
interpreter path (or let DjangoVista detect `.venv` or `venv`; it falls back to the
server's Python) and click **Check migrations**. The opened directory must contain
`manage.py`.

The panel runs `makemigrations --dry-run` to report model changes without a
migration, and `showmigrations --plan` to list applied and pending migrations. It
does not create migration files or apply migrations: run the displayed
`makemigrations` and `migrate` commands in your own terminal, then check again.

Checks are manual, and a source change marks the last result as stale. Database
errors are shown as errors, not as an up-to-date status. Each command has a 20-second
timeout.

## manage.py integration

To open DjangoVista from your project's terminal, install it into the same
environment as your project with the Django extra, then add it to `INSTALLED_APPS`:

```bash
pip install "django-vista[django]"
```

```python
INSTALLED_APPS = [
    # your apps ...
    "django_vista",
]
```

```bash
python manage.py showmodels
python manage.py showmodels --port 8001
python manage.py showmodels --no-browser
python manage.py showmodels --project /path/to/project
```

The command serves the diagram until you press Ctrl+C. It uses the directory
containing `manage.py`, falling back to `BASE_DIR` for programmatic invocation.
Django loads your installed apps when it runs any management command; the diagram
scan itself remains static. Startup does not run migration checks.

## Python library

```python
from pathlib import Path
from django_vista import build_graph

graph = build_graph(Path("/absolute/path/to/your/django-project"))
print(graph.model_dump_json(indent=2))
```

`build_graph` returns a Pydantic model with `nodes`, `edges` and `diagnostics`. See
the [graph contract](https://github.com/Jamshid057/Django-Vista/blob/main/docs/GRAPH_CONTRACT.md)
for exact semantics, including the HTTP and WebSocket protocol.

## Limitations

Static analysis cannot reproduce arbitrary Django runtime behavior.

- Dynamically generated models or fields, conditional definitions, wildcard imports
  and models defined only in unscanned dependencies are not supported.
- Custom field classes are reported as unsupported. Fields inherited from Django's
  own `AbstractUser`, `AbstractBaseUser` and `PermissionsMixin` are not displayed.
- `AUTH_USER_MODEL` is read from a literal assignment in scanned source. If it is
  missing or defined inconsistently, relations to the user model stay unresolved.
- Implicit `id` fields use a placeholder `AutoField` because `DEFAULT_AUTO_FIELD`
  is not evaluated.
- Abstract models are shown for context; inherited storage is represented through
  parent links.
- Every update rescans the project and sends a full snapshot. Large projects have not
  been benchmarked.
- Release validation is incomplete: browser acceptance and cross-platform wheel
  checks remain. See [validation results](https://github.com/Jamshid057/Django-Vista/blob/main/docs/VALIDATION.md).

## Development

### Install from source

Building the UI from source needs Python 3.12+ and **Node.js 24+**. Released wheels
do not.

```bash
git clone https://github.com/Jamshid057/Django-Vista.git
cd Django-Vista
python3 -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip
python scripts/build_release.py --bundle-only
python -m pip install .
django-vista serve /absolute/path/to/your/django-project
```

### Run backend and frontend separately

```bash
python -m pip install -e .
python -m uvicorn django_vista.main:app --reload
```

In a second terminal:

```bash
cd frontend
npm ci
npm run dev
```

Open `http://localhost:5173` and enter a project directory. The Vite dev server uses
`http://127.0.0.1:8000` as its API by default; set `VITE_API_URL` to change it (see
`frontend/.env.example`). The bundled UI connects to its own origin.
`examples/sample_project` contains parser fixtures for a demo. It is not a runnable
Django project and does not need Django installed.

### Tests and validation

```bash
PYTHONPATH=backend python -m unittest discover -s backend/tests -v
cd frontend
npm run test
npm run build
npm run lint
```

Backend tests cover parser behavior and in-process HTTP, WebSocket and watcher
lifecycle. CI runs these checks, builds the wheel and smoke-tests the installed
package on Python 3.12 and Node 24. Parser changes should come with a small source
fixture that asserts resolved edges and diagnostics.

### Build a release

```bash
python -m pip install build
python scripts/build_release.py
```

The script builds the frontend, copies it into the Python package and produces a
wheel and source archive in `dist/`. Use `--skip-install` when frontend dependencies
are already installed. Generated UI and build output are not committed.

### Naming

| Surface | Name |
| --- | --- |
| Project | DjangoVista |
| PyPI distribution, repository and terminal command | `django-vista` |
| Python import and Django app | `django_vista` |

The source lives in `backend/app/`; setuptools installs it as `django_vista`.

## Roadmap

Planned, **not implemented**:

- A VS Code extension that starts the local server and shows the diagram in an
  editor panel.
- A PyCharm integration after the VS Code extension.
- Field rename, delete and type changes, relationship editing and model creation.
- Incremental parsing, if measurements show it is needed.

See the [distribution roadmap](https://github.com/Jamshid057/Django-Vista/blob/main/docs/DISTRIBUTION.md)
and the historical [implementation plan](https://github.com/Jamshid057/Django-Vista/blob/main/PLAN.md).

## Contributing

Bug reports, documentation fixes and focused pull requests are welcome. Please read
[CONTRIBUTING.md](https://github.com/Jamshid057/Django-Vista/blob/main/CONTRIBUTING.md)
first, and report problems through
[GitHub Issues](https://github.com/Jamshid057/Django-Vista/issues). For connecting a
local checkout to a remote, see the
[Git setup guide](https://github.com/Jamshid057/Django-Vista/blob/main/docs/GIT_SETUP.md).

## License

Released under the [MIT License](https://github.com/Jamshid057/Django-Vista/blob/main/LICENSE).
