Django - Customise your SQL
summary
Django 1.7 adds lookups so custom operators (e.g. a true not-equals) compile into SQL; a hand-written lookup above saves about 30 microseconds versus Django’s negation form. Django 1.8 adds expressions to combine fields/aggregates; 1.10 adds date truncation so date_trunc quarter works for Postgres and produces the example report count of 506. For pivoting use Case/conditional SUMs; for quarter‑over‑quarter growth use Postgres window functions like LAG, executed via raw cursor SQL when the ORM can’t express the window.
AI-written summary · about
chapters (11)
- 0:00Introduction
- 1:28Recent changes
- 3:59Sales reporting
- 5:20Exclude
- 7:28Quarterly
- 11:43Quarterly aggregation
- 13:36Conditional expression
- 14:35Sum if
- 15:28Final query
- 16:27Growth report
- 18:50Querying directly
related talks
21mDjango and PostgreSQL: An Even-Closer Union
25mBuilding effective Django queries with expressions
16mBig O No: Django ORM runtime complexity and how to avoid it using LATERAL JOINS
35mDjango's GeneratedField
23mDjango with PostgreSQL superpowers
44mPostgreSQL in Django 1.8
22mDjango with PostgreSQL superpowers
46mPostgreSQL Beyond Django: Strategies to Get Max Performance with Álvaro Justen
