-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple.py
39 lines (29 loc) · 1003 Bytes
/
simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Some simple checks."""
import django
from django.db.models import Sum
from django_cte import With
django.setup()
from myapp.models import Order, Region # noqa: E402
Order.objects.all().delete()
Region.objects.all().delete()
region = Region.objects.create(name="example")
Order.objects.create(amount=2, region=region)
cte = With(
Order.objects
.values("region_id")
.annotate(total=Sum("amount")),
)
orders = (
# FROM orders INNER JOIN cte ON orders.region_id = cte.region_id
cte.join(Order, region=cte.col.region_id)
# Add `WITH ...` before `SELECT ... FROM orders ...`
.with_cte(cte)
# Annotate each Order with a "region_total"
.annotate(region_total=cte.col.total)
)
order: Order = orders.get()
assert isinstance(order, Order)
region_total: int = orders.get().region_total
assert isinstance(region_total, int)
# without strong typing of CTE, it can not know the type of region_total :
mypy_should_raise_but_it_does_not: str = orders.get().region_total