Skip to main content

Claude/ChatGPT Prompt to Optimise Django ORM Queries and Kill N+1

Find and fix slow Django ORM queries using select_related, prefetch_related, only(), and annotations, with before/after counts.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt frames the model as a senior Django engineer and asks for working before/after code, not generic ORM advice. You paste the slow [view] and your [toolbar_findings] from django-debug-toolbar, set the [traffic] level and [django_version], and it identifies each N+1 pattern, applies select_related/prefetch_related, trims columns with only()/values(), and replaces Python-side aggregation with database annotations.

The structure works because it targets real offenders instead of guessing. Feeding [toolbar_findings] grounds the fixes in the actual query list firing on the page, so the model collapses the relations that genuinely fire in a loop. [traffic] is used to rank the fixes by impact, so the busiest page's hotspots come first. [django_version] keeps the ORM syntax current. The deliverable to state expected query-count reduction per request makes the payoff measurable rather than hand-waved, and pasting the real view keeps the refactor aligned with code you actually run.

When to use it

  • A list view balloons to hundreds of queries because a template touches a relation per row.
  • You have django-debug-toolbar output and want fixes aimed at the real offenders.
  • You need before/after code for each hotspot, not generic advice to "use select_related".
  • You're doing Python-side aggregation loops that should be database annotations.
  • You're hauling unused columns and want only()/values() applied where it helps.
  • You want fixes ranked by page traffic so you tackle the heaviest pages first.
  • You want to drop to values() where a view needs columns, not full model instances.

Example output

You get the refactored view code with select_related, prefetch_related, only(), and annotations applied, before/after snippets for each hotspot, and a short table of query counts before and after per request. The table makes the win concrete -- e.g. a view dropping from hundreds of queries to a handful -- and the fixes are ranked so the highest-traffic page comes first, letting you spend effort where it returns the most.

Pro tips

  • Paste the actual [view] and the real [toolbar_findings]; the quality of the fixes depends on the model seeing the true query list.
  • Set [traffic] honestly so the ranking puts your busiest page's hotspots at the top.
  • Give the right [django_version] so the suggested ORM APIs match what you can run.
  • Reach for annotations over Python loops -- pushing aggregation into the database is usually the single biggest win on a heavy page.
  • Verify the before/after query counts yourself in debug-toolbar; the model's estimate is a guide, not a measurement.
  • Watch that only() doesn't strip a column the template later accesses, or you'll re-trigger queries you just removed.
  • Prefer values() over only() when the view just needs a few fields and no model instances downstream.

Frequently Asked Questions

Do I need to paste django-debug-toolbar output?
It works best when you do. The `[toolbar_findings]` variable grounds the fixes in the actual query list firing on the page, so the model targets the real offenders rather than guessing which relations cause the N+1 pattern.
Does it give before/after code or just advice?
It returns working before/after code for each hotspot plus a table of query counts before and after per request. The prompt explicitly demands concrete refactors, not generic ORM advice, so you can see exactly which lines change and the expected reduction.
How does it decide which fixes to prioritize?
The `[traffic]` variable is used to rank fixes by page traffic, so the busiest page's hotspots come first. Set it honestly so the ordering reflects real impact, since a fix on a rarely-hit page matters less than one on your heaviest list view.
Will it replace Python aggregation loops?
Yes. One deliverable is to replace Python-side aggregation loops with database annotations, which is usually the single biggest win on a heavy page. Verify the resulting query counts in debug-toolbar yourself, since the model's estimate is a guide rather than a measurement.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Django & Flask Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support