How to write a Case() SQL expression with an aggregated field (Sum) using Django’s query builder I am trying to recreate this SQL statement using Django’s query builder: