AGENTS.md templates / Django

AGENTS.md template for Django

A ready AGENTS.md for a Django project. It covers apps, models and migrations, the management commands agents should run, and the migration and query mistakes that are expensive to undo.

Updated 3 December 2026 · 69 lines · 3.0 KB
AGENTS.md
# AGENTS.md

A Django web application on Python 3.12 with PostgreSQL.

## Commands

- `uv run python manage.py runserver`: start the dev server on http://localhost:8000
- `uv run python manage.py makemigrations --check --dry-run`: confirm no model change is missing a migration
- `uv run python manage.py migrate`: apply migrations
- `uv run pytest`: run the tests (pytest-django)
- `uv run ruff check .`: lint

## Project structure

- `config/`: project settings, root URL configuration, ASGI/WSGI entry points
- `<app>/models.py`: models for one app
- `<app>/views.py, urls.py`: views and their routes
- `<app>/migrations/`: generated migrations. Never edit one that has been applied
- `templates/, static/`: templates and static assets

## Rules for agents

- Never edit a migration that has been applied. Make a new one.
- When a view or template touches a relation in a loop, use `select_related` or `prefetch_related`.
- Business rules live on the model or in a service module, not in views.
- Ask before changing anything in settings.

## Conventions

### Django

- Use class-based views for CRUD, function views for one-offs. Pick a lane per app.
- Model managers and QuerySets — don't put query logic in views.
- Migrations are sacred: never edit a merged migration. Squash only on release branches.
- For APIs use DRF serializers with explicit `fields`. Never `fields = '__all__'` on a model that might grow.

### Python

- Python 3.11+ features are fair game (PEP 695 generics in 3.12, `Self`, `LiteralString`).
- Type hints on every function signature. Use `mypy --strict` or `pyright` in CI.
- f-strings for formatting. Never `%` or `.format()` in new code.
- Prefer `pathlib.Path` over `os.path`. Use `match/case` where it clarifies branching.
- Dependency management: match the repo's tool (`uv`, `poetry`, or `pip-tools`). Don't mix.

### Pytest

- Fixtures over setUp/tearDown. Scope them appropriately (`function`, `module`, `session`).
- Parametrize repetitive tests with `@pytest.mark.parametrize`.
- Mark slow / integration tests and keep them out of the default run.

### PostgreSQL

- UUIDv7 or bigint identity for PKs — never auto-increment integers for user-visible IDs.
- Indexes on every foreign key and on columns used in WHERE/ORDER BY. Check with `EXPLAIN ANALYZE`.
- Use `jsonb` (not `json`). Add GIN indexes only when you query into it.
- Transactions for multi-row writes. Be explicit about isolation level when it matters.

## Avoid

- Bare `except:` clauses — always catch a specific exception.
- Mutable default arguments (`def f(x=[])`).

## Guardrails

- Never commit secrets, API keys, or credentials. If you spot one in a diff, stop and flag it.
- Do not invent dependencies. If a package is not already in the manifest, confirm before adding it.
- When a command fails, read the actual error. Do not retry blindly or bypass with `--force`.
- Prefer editing existing files over creating new ones.
- Run the tests and the linter before saying a task is done, and say what you ran.

The commands and folders are the common defaults for Django. Change them to match your project before you commit the file: an agent will run exactly what is written here.

Make it yours in the generator Add your database, test runner and other tools, pick your coding style, and get matching CLAUDE.md, Cursor and Copilot files. Free, runs in your browser. Already have instruction files? Context Doctor scans your repo and shows which files each coding tool is silently not loading. $19.

How to use it

Save the file as AGENTS.md in the root of your repository and commit it. Then check each tool you use:

ToolWhat to do
Claude CodeReads AGENTS.md only when no CLAUDE.md exists in or above the folder. If you have a CLAUDE.md, put @AGENTS.md on its first line.
CursorReads AGENTS.md from the project root, in the desktop app and the terminal. In the app, rules in .cursor/rules must be .mdc files. The terminal reads only plain .md files there.
CodexReads AGENTS.md from the root down to the folder you are in, with a shared 32 KiB limit across all of them.
GitHub CopilotReads AGENTS.md on some surfaces only. Keep a .github/copilot-instructions.md as well if your team relies on Copilot.

Details and sources: the AGENTS.md compatibility matrix.

What agents get wrong in Django projects

These are the reasons the “Rules for agents” section says what it says.

Editing an applied migration

To make a model change "clean", agents rewrite an existing migration file. It works on their empty database and breaks every environment that already ran it. The template says new migration, always.

N+1 queries in views and templates

Generated views loop over a queryset and touch a relation on each row. The fix is one select_related or prefetch_related, and the template asks for it.

Logic in views

Agents put everything in the view function. The template points business rules at the model or a service module.

Changing settings to make a test pass

Turning off a check or adding a permissive default in settings is a common shortcut. Settings are listed as ask-first.

Questions

Where does AGENTS.md go in a Django project?

In the root of the repository, next to your manifest file. Tools read it from the folder you start them in and, for most tools, from parent folders too.

Does Claude Code read AGENTS.md?

Only when there is no CLAUDE.md in or above the working folder. If you already have a CLAUDE.md, keep it and make its first line @AGENTS.md so both tools read the same rules.

How long should the file be?

Short enough that every line is something the agent would get wrong without it. Codex shares a 32 KiB budget across all the AGENTS.md files it loads, and long files get followed less closely by every tool.

Does this cover Django REST Framework?

Add two lines if you use it: where serializers live, and that every viewset declares its permission classes explicitly.

Templates for other stacks

Next.jsReactNode.js and TypeScriptExpressPythonFastAPIGoRuby on RailsLaravelVueSvelteKitAstroNestJSFastifyHonoFlaskRustElectronSupabase