Home › Django › Django models & migrations .claude/rules file

Django models & migrations .claude/rules file

A path-scoped Claude Code rules file for Django models, migrations, and ORM queries, loaded only for those files.

Claude Code (CLAUDE.md)Django

How to use

Save this as .claude/rules/django-models.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.claude/rules/django-models.mdDownload  
---
paths:
  - "**/models.py"
  - "**/models/*.py"
  - "**/migrations/*.py"
  - "**/services.py"
---

# Django models, migrations and queries

- After changing a model, run `python manage.py makemigrations <app>` and read the generated file. Never hand-edit an applied migration; write a new one.
- Data migrations use `RunPython(forward, backward)` with `apps.get_model(...)`, never direct model imports.
- Adding a non-null column to a big table: add it nullable or with `db_default`, backfill in batches, then tighten.
- Every `ForeignKey` sets `on_delete` deliberately and a `related_name`.
- Business invariants as DB constraints in `Meta.constraints` (`UniqueConstraint`, `CheckConstraint`), not only in Python.
- Index fields used in filters/order (`Meta.indexes`), and say why in the PR.
- Query rules:
  - `select_related` for FK/one-to-one, `prefetch_related` for reverse/M2M.
  - `F()` for atomic updates (`stock=F("stock") - 1`), `select_for_update()` inside `transaction.atomic()` for read-then-write.
  - `bulk_create`/`bulk_update` for batches; `.iterator()` for large scans.
  - `exists()` instead of `count() > 0`.
- `__str__` on every model; `get_absolute_url` where there's a detail page.
- Don't put request/HTTP logic in models.

Check with `python manage.py makemigrations --check --dry-run` and the test suite.

More: all Django rules · all Claude Code (CLAUDE.md) rules

Related rules

Django CLAUDE.md

CLAUDE.md for Django 5 projects with DRF, pytest-django and PostgreSQL.

Claude Code (CLAUDE.md)Django

Django Ninja API rules

Cursor rules for Django Ninja APIs on Django 5.2: routers, Pydantic schemas, auth, async views, and tests.

CursorDjango

Django REST Framework rules

Cursor rules for Django 5 + Django REST Framework APIs: serializers, viewsets, permissions, and query performance.

CursorDjango

Django Windsurf rules

Windsurf rules for Django 5.2 LTS projects: apps, models and migrations, ORM performance, forms, and tests.

WindsurfDjango

Bun runtime CLAUDE.md

CLAUDE.md for TypeScript projects on Bun 1.2+: bun test, Bun.serve, built-in SQLite/Postgres, and bundling.

Claude Code (CLAUDE.md)Bun

Elixir Phoenix CLAUDE.md

CLAUDE.md for Phoenix 1.8 + LiveView 1.x apps in Elixir: contexts, Ecto changesets, and ExUnit tests.

Claude Code (CLAUDE.md)Elixir / Phoenix

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.