Can we do arithmetic using Django Subqueries?
问题 I am wondering if Django's ORM allows us to do aggregate operations on subqueires, and then do arithmetic with the resulting values. What would be the proper way to go about something like this: record = PackingRecord.objects.filter(product=OuterRef('pk')) packed = FifoLink.objects.filter(packing_record__product=OuterRef('pk')) output = obj_set.annotate( in_stock=(Subquery(record.aggregate(Sum('qty'))) - Subquery(packed.aggregate(Sum('sale__qty')))) ).values('id', 'name', 'in_stock') 回答1: You