About django-db-side-computation
django-db-side-computation is a skill for Django developers moving suitable calculations from Python into the database. It guides ORM expression choices, query-plan checks, and decisions about storing results, with correctness and performance verification.
Key features
- 01Identify computations suited to the database
- 02Choose Django ORM expressions
- 03Inspect SQL and query plans
- 04Assess indexes and result storage
- 05Compare correctness and runtime
Use cases
- 01Replace related-object counts with database aggregates
- 02Fetch the latest related value per row
- 03Evaluate ranking queries with window functions
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ lvtd-llc/ skills --skill django-db-side-computationAfter installing, type/django-db-side-computationin Claude Code to confirm it loaded.
Bundled filesreferences/aggregation-patterns.md
Details
From the project READMEDjango DB-Side Computation
Use this skill when Django fetches rows into Python to compute values the database can compute more efficiently or more consistently.
Workflow
-
Identify the Python computation.
- Counts, sums, min/max, booleans, latest related rows, rankings, denormalized totals, or per-row derived fields.
- Confirm whether the computed value must be exact, current, and transactionally consistent.
-
Pick the SQL expression level.
filter(),exclude(), andF()for simple comparisons and updates.annotate()and aggregates for per-object counts and totals.- Filtered aggregates for conditional counts and sums.
Exists()for yes/no related-row checks.Subquery()withOuterRef()for latest or scalar related values.Window()for ranks and running calculations.GeneratedFieldfor deterministic same-row computed columns when the database supports it.
-
Validate query shape.
- Inspect generated SQL or
QuerySet.explain(). - Clear unintended ordering before grouping when needed.
- Confirm indexes support joins, filters, and ordering.
- Inspect generated SQL or
-
Decide whether the result should be stored.
- Use annotations for request-time values.
- Use generated fields for same-row deterministic values.
- Use materialized views or denormalized tables when cross-row aggregate queries are too expensive and can be stale.
See aggregation-patterns.md for Django expression examples.
Safety Notes
- Database computation is not always faster; complex queries can overload the database or block OLTP work.
- Generated fields have database-specific restrictions and PostgreSQL supports only persisted generated columns.
- Subqueries and annotations can duplicate work if composed carelessly. Read the plan.
Verification
Compare correctness against the old Python calculation on representative data, then measure SQL time and total response time.