Skip to content
Back to skills

Django Orm Optimization

ASecurity

Finds slow Django queries and N+1 patterns, then fixes them with select_related, prefetch_related, only and indexes, with query counts as proof.

  • 3 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added November 15, 2026
ai-agentsgosqldjangodatabase

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill django-orm-optimization --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Django Orm Optimization?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Django Orm Optimization
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-django-orm-optimization/badge)](https://www.skillsdirectory.com/skills/workspace-templates-django-orm-optimization)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: django-orm-optimization
description: "Finds slow Django queries and N+1 patterns, then fixes them with select_related, prefetch_related, only and indexes, with query counts as proof."
---

# Django ORM optimization

Count queries, fix the worst one, count again.

## Steps

1. Measure one slow view or serializer. Use `assertNumQueries` in a scratch test, `django-debug-toolbar`, or `connection.queries` with `CaptureQueriesContext`. Record the count and the slowest statements.
2. Find the pattern in the code: attribute access on a foreign key inside a loop or template (use `select_related`), reverse or many-to-many access in a loop (use `prefetch_related`), serializers with nested fields.
3. Apply the narrowest fix. `select_related("customer")` for forward foreign keys and one-to-one. `prefetch_related(Prefetch("items", queryset=Item.objects.select_related("product")))` for collections, and filter inside `Prefetch` rather than filtering after.
4. Load fewer columns on wide tables with `only()` or `defer()`; use `values()` or `values_list()` when you do not need model instances.
5. Move work to the database: `annotate(Count(...))`, `Exists()` instead of `len(qs)` or `if qs`, `bulk_create` and `bulk_update` for loops of saves, `update()` for set-based changes.
6. Check the plan of the slow statement with `qs.explain(analyze=True)`. Add an index with `Meta.indexes` that matches the filter and ordering columns, in a migration; use `AddIndexConcurrently` on PostgreSQL for big tables.
7. Pin the result in a test with `assertNumQueries(n)` for the view.

## Rules

- Never add `select_related` across many-valued relations; it multiplies rows.
- Never use `.count()` and then iterate the same queryset; evaluate once.
- Do not add an index without evidence from the plan.

## Report

Queries before and after, the statements changed, indexes added and the test that guards them.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.