django-orm-query-optimization

Installation
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

  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.
Installs
46
Repository
lvtd-llc/skills
GitHub Stars
1
First Seen
Jun 21, 2026
django-orm-query-optimization — lvtd-llc/skills