Order serialization N+1 queries
Review this Python (Django ORM) view that serializes orders with their line items.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 25 min
Mark a line and say what kind of problem it is.0 findings
1def order_report(request):
2 orders = Order.objects.filter(
3 created__gte=last_month()
4 ).order_by("-created")
5 rows = []
6 for order in orders:
7 rows.append({
8 "id": order.id,
9 "customer": order.customer.name,
10 "total": sum(item.qty * item.unit_price
11 for item in order.items.all()),
12 "warehouse": order.shipment.warehouse.code,
13 })
14 return JsonResponse({"rows": rows})
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.