Claude Code Prompts for Django Projects

Copy-paste Claude Code prompts for Django 5 models, safe migrations, DRF serializers, N+1 query fixes, and select_related optimisation. Battle-tested 2026.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Django's strength — convention over configuration — is exactly what makes Claude Code so effective on Django repos: the conventions act as guardrails. The prompts below give Claude the right scope so it builds models, migrations, and DRF endpoints that match your existing patterns rather than freelancing.

Prompt 1 — The "add a new model end-to-end" prompt

Add a new model 'Invoice' to the billing app.

Fields:
- customer (FK to customers.Customer, on_delete=PROTECT)
- amount_cents (PositiveIntegerField)
- currency (CharField(3), choices=billing.currencies.ISO_4217)
- status (CharField, choices=billing.invoice_status.STATUSES, default='draft')
- issued_at (DateTimeField, null=True)
- created_at, updated_at (auto)

Constraints:
- Read billing/models.py first for the existing pattern (TimeStampedModel base?).
- Match indentation, import order, and field ordering used by the existing Charge model.
- Add a Meta.indexes entry on (customer, status) — that's the hot read path.
- Generate the migration with --name add_invoice. Do NOT run migrate.
- Add a basic admin.py registration matching the existing Charge admin.
- Add a DRF ModelSerializer in billing/serializers.py and ViewSet in views.py
  registered in urls.py — same patterns as ChargeViewSet.

Do not write tests yet; I'll prompt for those after I review the diff.

Why it works: the "read X first" line forces Claude to load the existing pattern before generating. Without it, Claude defaults to a generic Django style that may not match your project's conventions.

Prompt 2 — Zero-downtime migration prompt

I need to rename the column 'amt' to 'amount_cents' on billing.Invoice in
production without downtime. There are ~12M rows.

Produce a sequence of migrations that:
1. Adds the new column 'amount_cents' nullable, no default (cheap).
2. Backfills in a separate RunPython migration with .iterator(chunk_size=5000)
   to avoid memory blowup. Wrap in transaction.atomic per chunk.
3. Sets NOT NULL on the new column.
4. Updates the Django model field to point at the new column.
5. Drops the old column (separate migration — deploy step 4 first).

Each step = one migration file. Number them so I can deploy 1-2-3 → release → 4 → release → 5.
Do not combine. Do not use AlterField rename — that takes a write lock on Postgres.

Prompt 3 — The "fix this N+1" prompt

Profile the InvoiceListView (billing/views.py) — django-debug-toolbar shows
~140 queries for a 20-row page. Find the N+1 sources.

Constraints:
- Read the serializer used by the view first.
- For each related access (FK, M2M, reverse), decide select_related vs prefetch_related.
- Output a unified diff for ONE file at a time. Show the queryset change and explain
  in 1 line why select vs prefetch.
- Verify by mentally walking through: with these joins, how many queries remain?
  State that number.

Tips that move the needle on Django repos

Related: calling the Claude API from Django, testing prompts, refactoring prompts.

Frequently asked questions

Will Claude Code respect my Django app boundaries?
Only if you tell it. Add a line to CLAUDE.md saying 'Models live in /models.py — never put models in views or serializers. Cross-app foreign keys must use string references (app_label.Model) to avoid circular imports.' Claude follows explicit boundary rules well.
Can Claude write zero-downtime Django migrations?
Yes, if you constrain it. Use the migration prompt below — it forces Claude to produce the rename-then-drop pattern (add column nullable, backfill, set NOT NULL, drop old) rather than a single destructive AlterField.
How do I stop Claude from suggesting fat views?
Put `## View layer` rules in CLAUDE.md: 'Views ≤ 30 lines. Business logic belongs in services/ or model methods, never the view body.' Claude reads CLAUDE.md every session and respects per-layer caps.
Does Claude know Django 5 idioms vs older Django?
Yes — its training covers Django 5. But if your project still runs 4.2 LTS, say so in CLAUDE.md (`Django version: 4.2 LTS — no Django 5 syntax`) or Claude will sometimes use newer field types that break older versions.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript