django-db-side-computation
Move Django performance-sensitive Python loops into database-side computation with annotations, aggregates, filtered aggregates, F expressions, functions, Subquery, OuterRef, Exists, Window expressions, db_default, and GeneratedField. Use when Django code counts, sums, filters, flags, ranks, or derives values in Python after fetching rows.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 491
01 前言 Front Matter
- name
- django-db-side-computation
- license
- MIT
- metadata
- { "tags": "django,database,aggregation,orm,analytics", "version": "0.1.0", "category": "Django", "displayName": "Django DB-Side Computation" }
- description
- Move Django performance-sensitive Python loops into database-side computation with annotations, aggregates, filtered aggregates, F expressions, functions, Subquery, OuterRef, Exists, Window expressions, db_default, and GeneratedField. Use when Django code counts, sums, filters, flags, ranks, or derives values in Python after fetching rows.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 1
- 参考文档references/aggregation-patterns.md2.5 KB
03 SKILL.md 正文
Django 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.
04 同仓库更多 Skills
worktable-review-with-annotations
Create, inspect, reply to, update, or resolve situated Worktable annotations. Use when feedback belongs to a Doc section, block, text range, or HTML Doc, or when the user asks to address existing review feedback and coordinate a requested artifact edit.
worktable-find-and-synthesize-context
Find, assess, and synthesize context from Worktable Docs and Records. Use when the user asks what a workspace contains, wants workspace-backed research, needs current facts reconciled across artifacts, or requests a cited synthesis without changing workspace state.
worktable-create-or-update-docs
Create, edit, rename, or maintain narrative Worktable Docs. Use for plans, decisions, briefs, notes, research syntheses, and other durable prose when the user requests a new artifact or a safe update to an existing one.
worktable-create-or-manage-records
Organize repeated structured information as durable Worktable Records. Use for research datasets, inventories, directories, pipelines, trackers, and other collections whose items need identity, fields, filtering, links, validation, or individual updates.
worktable-create-or-manage-interactive-html
Create or improve durable visual and interactive artifacts in Worktable. Use for dashboards, trackers, calculators, mini apps, slide presentations, and other requests that benefit from a polished HTML Doc.
worktable-collaborate-threads
Read, answer, and hand off messages through durable Worktable threads with users or other authenticated participants. Use when the user asks to check addressed messages, reply to a participant, continue a thread, request another agent's help, wait briefly for a response, or watch for comments and messages sent to the agent.