python
36 lines · 7 steps
Build a monthly revenue report in Django
Grouping and aggregating orders by month entirely in SQL with Django's ORM.
Explained by
highlit
1from django.db.models import Sum, Count, F, DecimalField
2from django.db.models.functions import TruncMonth, Coalesce
3
4from .models import Order
5
6
7def monthly_revenue_report(year, *, status="paid"):
8 queryset = (
9 Order.objects.filter(
10 placed_at__year=year,
11 status=status,
12 )
13 .annotate(month=TruncMonth("placed_at"))
14 .values("month")
15 .annotate(
16 gross_revenue=Coalesce(
17 Sum(F("line_items__unit_price") * F("line_items__quantity")),
18 0,
19 output_field=DecimalField(max_digits=12, decimal_places=2),
20 ),
21 refunds=Coalesce(Sum("refunds__amount"), 0, output_field=DecimalField()),
22 order_count=Count("id", distinct=True),
23 )
24 .annotate(net_revenue=F("gross_revenue") - F("refunds"))
25 .order_by("month")
26 )
27
28 return [
29 {
30 "month": row["month"].strftime("%Y-%m"),
31 "orders": row["order_count"],
32 "gross": row["gross_revenue"],
33 "net": row["net_revenue"],
34 }
35 for row in queryset
36 ]
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Chaining values() then annotate() turns the ORM into a SQL GROUP BY over the chosen column.
- 2Coalesce guards aggregates against NULL so empty sums return a usable zero instead of None.
- 3Pushing arithmetic into F expressions keeps computation in the database rather than in Python.
Related explainers
python
import argparse import sys from pathlib import Path
Building a subcommand CLI with argparse
cli
argparse
subcommands
Intermediate
6 steps
python
from collections.abc import Mapping from typing import Any, Iterator
Flattening nested config into dotted keys
recursion
generators
tree-traversal
Intermediate
7 steps
python
import csv import io from datetime import datetime
Streaming a CSV export in Flask
streaming
generators
csv
Intermediate
9 steps
python
import time from collections import defaultdict from threading import Lock
Sliding-window login rate limiting in Flask
rate-limiting
sliding-window
thread-safety
Intermediate
7 steps
python
from django.conf import settings from django.contrib.auth import get_user_model from django.core.mail import EmailMultiAlternatives from django.db.models.signals import post_save
Sending a welcome email with Django signals
signals
email
user-activation
Intermediate
8 steps
java
@Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = StrongPasswordValidator.class) @Documented
Building a custom @StrongPassword validator in Spring
bean-validation
annotations
regex
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/build-a-monthly-revenue-report-in-django-explained-python-310e/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.