# Django ORM Lens: N+1, Migration Safety & Schema Map

> Your agent stops guessing about Django: real N+1 detection, migration-risk review, schema drift and "what breaks if I drop this field", from static analysis. No DB, no settings.

Django ORM Lens: N+1, Migration Safety & Schema Map is a free AI agent skill on Agensi by FROWNINGdev, in Data & Databases. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [FROWNINGdev](https://www.agensi.io/creators/frowningdev)
- Category: [Data & Databases](https://www.agensi.io/skills/data-databases)
- Uses: 3
- Version: 1.2
- Last updated: 2026-10-28
- Security: scan passed (2026-10-28); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/django-orm-lens-n-1-migration-safety-schema-map
- Tags: database, code-review, api, django, python, migrations, orm, postgres

## About this skill

Most Django skills are advice. This one ships an engine.

Ask your agent "is this migration safe to deploy?" and it normally skims a few files and guesses. With this skill it runs **django-orm-lens**, an open-source static analyzer (MIT, on PyPI, ~90★ on GitHub), across the whole project and answers from facts:

- **Migration review that blocks real outages.** 16 risk rules: NOT NULL column without a default, `AddIndex` that locks a Postgres table, renames that break rolling deploys, irreversible `RunPython`, conflicting leaves. Each finding comes with the safe rewrite (three-step NOT NULL, `AddIndexConcurrently`, expand/contract).
- **Blast radius.** For every destructive operation: the code that still reads the dropped column, grouped by Django layer (serializers, forms, admin, views, templates, tests) and tagged certain / likely / possibly.
- **Schema drift.** `makemigrations --check` without a settings module: fields declared in models.py that no migration creates — the column that will be missing at runtime.
- **N+1 detection.** Loops over querysets (sliced ones and nested loops included) that touch FK / M2M / reverse relations without `select_related` / `prefetch_related`, with the exact fix and the full relation path (`product__category`) — then the agent re-runs the scan to prove the fix worked.
- **Safe field / model removal.** `impact` lists every reference before you touch anything, and the skill turns it into a two-release deploy plan.
- **Cascade check.** What deleting one row takes with it, walked recursively — "deleting a Category erases order history" before it happens.
- **Schema map.** ER diagrams (Mermaid, DBML, D2, PlantUML, Graphviz), model details, relations with `on_delete`, signal graph, migration DAG, index suggestions from real query usage.

**Safe on any repo.** Pure AST parsing: it never imports your project, never needs `DJANGO_SETTINGS_MODULE`, secrets, installed dependencies or a database. Works on a fresh clone or a PR branch.

**Works everywhere.** Uses the django-orm-lens MCP server (13 tools) when connected, otherwise the CLI via `uvx` — zero install. Includes a one-shot `audit.py` that merges all analyzers into one verdict (BLOCK / FIX BEFORE DEPLOY / SHIP), with a `--changed-since origin/main` mode for PR review.

**Honest by design.** The skill tells the agent to check renames under the old name, verify name-matched references, walk cascades past the first level, do a short manual N+1 pass for what static detection cannot see, separate what a PR introduced from what was already broken, and say exactly what was checked instead of "no issues".

### What's inside

- `SKILL.md` — six playbooks (schema map, migration review, safe schema change, N+1, cascade, signals / migration history) and a report template
- `references/migration-playbook.md` — all 16 rules with copy-paste safe migrations and deploy order
- `references/tools.md` — every MCP tool and CLI command, MCP install lines for Claude Code, Cursor, VS Code, Codex, Gemini CLI, Windsurf, Claude Desktop
- `scripts/audit.py` — one-command audit

Built by the author of django-orm-lens (VS Code extension, CLI, MCP server). Source: https://github.com/FROWNINGdev/django-orm-lens

## Requires

- Python 3.9+
- uv (Python tool runner) or pipx or pip
- django-orm-lens
- Optional: Git
- Optional: django-orm-lens MCP

django-orm-lens is free on PyPI; uvx fetches it automatically. Git is only for PR-scoped audits.

## Known limitations

Static analysis, so it is a floor, not a ceiling: lookups built dynamically (getattr, **kwargs), raw SQL strings and querysets assembled across many functions can hide references and N+1 loops. drift compares fields, not Meta.indexes. cascade_preview reports one level (the skill walks it recursively). Templates and code outside the project root are not scanned. The skill tells the agent to name these gaps in its report and recommends one runtime check (makemigrations --check, assertNumQueries) when the stakes are high.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Django ORM Lens: N+1, Migration Safety & Schema Map skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### Does it need my database or settings?

No. It parses source code with Python's AST: no DJANGO_SETTINGS_MODULE, no django.setup(), no database connection, no installed project dependencies. It is safe on a fresh clone or a PR branch.

### Do I have to install anything?

If you have uv, nothing: the skill runs `uvx django-orm-lens`. Otherwise `pip install django-orm-lens`. For the MCP server it is one command: `claude mcp add django-orm-lens -- uvx --from "django-orm-lens[mcp]" django-orm-lens-mcp`.

### Which agents does it work with?

Any agent that supports SKILL.md and can run a shell command: Claude Code, Cursor, Codex CLI, Gemini CLI, GitHub Copilot, Windsurf and others. With an MCP-capable agent it uses the 13 django-orm-lens MCP tools directly.

### How is this different from just asking my agent to review migrations?

The agent stops guessing. Every finding comes from a deterministic scan of the whole project with a file and line, the skill gives the safe rewrite for each risk, and it makes the agent re-run the scan after a fix to prove it worked.

### Can I use it in CI?

Yes. The same engine has `--format github` (PR annotations) and `--format sarif` output, and the bundled `audit.py --changed-since origin/main` reviews only the migrations in a PR and returns BLOCK / FIX BEFORE DEPLOY / SHIP.

### Which Django versions are supported?

It reads source, so any Django version with the standard models.py / migrations layout. The engine is regression-tested against real projects such as Saleor, Wagtail, Zulip and django-cms.

### Is it really free?

Yes. The skill and the django-orm-lens engine are free and MIT-licensed. Source: github.com/FROWNINGdev/django-orm-lens

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
