django-orm-query-optimization
Optimize Django ORM query loading by fixing N+1 queries, duplicate related-object queries, over-fetching, serializer/template query loops, and inappropriate select_related, prefetch_related, only, or defer usage. Use when a Django page, API, admin view, template, serializer, or queryset issues too many queries or loads more model data than needed.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 490
01 前言 Front Matter
- name
- django-orm-query-optimization
- license
- MIT
- metadata
- { "tags": "django,orm,querysets,performance,n-plus-one", "version": "0.1.0", "category": "Django", "displayName": "Django ORM Query Optimization" }
- description
- Optimize Django ORM query loading by fixing N+1 queries, duplicate related-object queries, over-fetching, serializer/template query loops, and inappropriate select_related, prefetch_related, only, or defer usage. Use when a Django page, API, admin view, template, serializer, or queryset issues too many queries or loads more model data than needed.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 1
03 SKILL.md 正文
Django ORM Query Optimization
Use this skill for query-count and object-loading problems. The usual target is an N+1 pattern where each rendered row or serialized object triggers more database work.
Workflow
-
Prove the query pattern.
- Capture query count and duplicate SQL for the slow path.
- Identify the loop, serializer field, template access, admin display, or model property triggering related queries.
-
Choose the loading strategy.
- Use
select_related()for single-valued relationships:ForeignKeyandOneToOneField. - Use
prefetch_related()for many-valued relationships: reverse FK and many-to-many. - Use
Prefetch()when the related queryset needs filters, ordering, annotations, or nestedselect_related(). - Use
values()orvalues_list()when model instances are unnecessary. - Use
only()ordefer()only when large columns are truly unused.
- Use
-
Keep access consistent.
- If you prefetch
items, do not later callitems.filter(...)and expect the prefetch cache to help. - If using
iterator()withprefetch_related(), providechunk_size. - Avoid putting broad prefetches into default managers unless every caller needs them.
- If you prefetch
-
Add a focused regression check when useful.
- Use a query-count assertion around the exact view or serializer path.
- Avoid brittle global query-count tests.
See relationship-and-column-loading.md for examples and review cues.
Anti-Patterns
- Adding
prefetch_related()to every queryset without checking memory. - Using
only()and then touching deferred fields in a loop. - Optimizing a queryset but leaving serializer method fields to issue per-row queries.
- Hiding queries in
__str__(), model properties, template includes, or admin list methods.
Verification
Show the query count and duplicate-query pattern before and after, plus any wall-time or memory change.
04 同仓库更多 Skills
migrate-to-e2e
Moves an existing Playwright, Cypress, Selenium, Appium, Detox, Maestro, or TesterArmy suite to e2e one slice at a time, with the old suite running beside the new one until each ported test is proven. Keeps a MIGRATION.md progress file so any later session resumes where the last stopped. Optionally plants a defect to show both suites catch it. Use for /migrate-to-e2e, "migrate our tests to e2e", "port these Maestro flows", "move our TesterArmy tests", or when a project has an e2e config and an older suite still running.
e2e
Writes, runs, and debugs end-to-end tests for web and mobile apps with e2e, the agentic test runner, which mixes agent.act goals with exact locator assertions on a Playwright browser or an iOS or Android device. Also drives a project's running app live over the e2e MCP server to look at a screen, check a UI change, take a screenshot or record a video, and try a locator; and runs bug bashes, parallel e2e explore runs with each bug proven by a repro test. Use when a project depends on e2e or has an e2e.config.ts, when asked for end-to-end, browser, mobile, or agentic UI tests, to check a change in the running app, to QA or bug bash an app or branch, or when an e2e run fails.
create-verification-skill
Generate a project-local verify-<app> skill on top of e2e, so any coding agent can launch the app, check it is worth driving, exercise a feature the way a user does, keep evidence, and bug bash it. Writes the skill, a feature map with one explore charter per feature, and proves it once. Use for /create-verification-skill, "make a verification skill for this repo", or when a project has no scripted way to prove UI behavior.
deja-search
deja-vu memory — search the user's past AI coding sessions with the deja CLI. Holds what they already settled and never wrote into the repo (naming rules, banned or already-tried tools, required checks before merge, preferred values, who owns what). Use before your first edit in a task, before you call a change done or ready to merge, before you recommend a tool, library, design or reviewer, before re-debugging an error that may already be solved, when they say 'didn't we fix this before', 'what did we decide about X' or 'I already have that', and before telling them something on this machine does not exist.
deja-reflect
End-of-session reflection and memory curation with deja-vu. Review what was decided, what commands broke and what repaired them, and record durable project rules or decisions with deja remember so the next session starts with them.
revid
Make, edit, export and post AI videos with Revid (revid.ai) through the Revid MCP server. Use whenever the user wants a short-form or long-form video made (from an idea, a script, a URL, an article, a PDF, a tweet, a product page, their own clips, or a song), wants to find what is going viral on TikTok, YouTube, Instagram or X before making something, wants to edit or re-export an existing Revid project, generate a standalone AI image or clip, make a series episode, or publish and schedule a finished video to connected social accounts. Covers setup (hosted MCP with OAuth, or an API key), the 36 tools, credits, polling and the rules for publishing. Routes to the more specific revid-* skills when one fits.