Skip to content
Skillv1.0.0

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

by hashgraph-online(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from hashgraph-online/awesome-codex-plugins (plugins/LVTD-LLC/skills/skills/django-orm-query-optimization/SKILL.md). Install upstream with npx skills add hashgraph-online/awesome-codex-plugins --skill django-orm-query-optimization. Copyright stays with the author (MIT).

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

  1. 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.
  2. Choose the loading strategy.

    • Use select_related() for single-valued relationships: ForeignKey and OneToOneField.
    • Use prefetch_related() for many-valued relationships: reverse FK and many-to-many.
    • Use Prefetch() when the related queryset needs filters, ordering, annotations, or nested select_related().
    • Use values() or values_list() when model instances are unnecessary.
    • Use only() or defer() only when large columns are truly unused.
  3. Keep access consistent.

    • If you prefetch items, do not later call items.filter(...) and expect the prefetch cache to help.
    • If using iterator() with prefetch_related(), provide chunk_size.
    • Avoid putting broad prefetches into default managers unless every caller needs them.
  4. 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.

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/hashgraph-online-awesome-codex-plugins-django-orm-query-dfd0bb/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

hashgraph-online-awesome-codex-plugins-django-orm-query-dfd0bb.ocm.jsonjson
{
  "ocm": "1",
  "id": "hashgraph-online-awesome-codex-plugins-django-orm-query-dfd0bb",
  "kind": "skill",
  "name": "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.",
  "publisher": "hashgraph-online",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "data_analysis",
      "coding"
    ],
    "tags": [
      "skill-md",
      "django",
      "orm",
      "querysets",
      "performance",
      "n-plus-one",
      "github"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "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."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "github",
      "repository": "https://github.com/hashgraph-online/awesome-codex-plugins",
      "path": "plugins/LVTD-LLC/skills/skills/django-orm-query-optimization/SKILL.md",
      "ref": "a964b7b7dbd11ed720c9ea2282d8c78e5641e987",
      "url": "https://github.com/hashgraph-online/awesome-codex-plugins/blob/a964b7b7dbd11ed720c9ea2282d8c78e5641e987/plugins/LVTD-LLC/skills/skills/django-orm-query-optimization/SKILL.md",
      "key": "hashgraph-online/awesome-codex-plugins/plugins/LVTD-LLC/skills/skills/django-orm-query-optimization/SKILL.md"
    },
    "compatibility": "Codex, Claude Code, and other Agent Skills-compatible clients.",
    "license": "MIT"
  },
  "instructions": "# Django ORM Query Optimization\n\nUse 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.\n\n## Workflow\n\n1. Prove the query pattern.\n   - Capture query count and duplicate SQL for the slow path.\n   - Identify the loop, serializer field, template access, admin display, or model property triggering related queries.\n\n2. Choose the loading strategy.\n   - Use `select_related()` for single-valued relationships: `ForeignKey` and `OneToOneField`.\n   - Use `prefetch_related()` for many-valued ",
  "cost": {
    "context_tokens": 488
  }
}

Fetch it by URL: GET /api/v1/registry/hashgraph-online-awesome-codex-plugins-django-orm-query-dfd0bb/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.