Code review exercise: spot the problems in an apply_discount view

Backend Live Coding November 13, 2026 python

A common exercise: the interviewer shows code and asks "what would you comment on in this pull request?".

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# ======================================================================
# Q: Code review: find the problems.
#
# A common exercise: the interviewer shows code and asks "what would you comment
# on in this pull request?". Find as many issues as you can before reading the
# answer.
# ======================================================================

@api_view(['POST'])
def apply_discount(request):
    order = Order.objects.get(id=request.data['order_id'])
    code = request.data['code']
    coupon = Coupon.objects.raw(f"SELECT * FROM coupon WHERE code = '{code}'")[0]
    if coupon.uses < coupon.max_uses:
        order.total = order.total - order.total * coupon.percent / 100
        order.save()
        coupon.uses += 1
        coupon.save()
    for item in order.items.all():
        print(item.product.name)
    return Response({'total': float(order.total)})