Perform a Left Outer Join with a CTE
mainWhile Django does not provide granular control over join types, you can perform a LEFT OUTER JOIN using the experimental _join_type argument in CTE.join(...).
WARNING: This feature is experimental. Django might automatically convert a LEFT OUTER JOIN to an INNER JOIN during query construction. Always verify the generated SQL.
from django.db.models.sql.constants import LOUTER
totals = CTE(
Order.objects
.values("region_id")
.annotate(total=Sum("amount"))
.filter(total__gt=100)
)
orders = with_cte(
totals,
select=totals
.join(Order, region=totals.col.region_id, _join_type=LOUTER)
.annotate(region_total=totals.col.total)
)