But if you post what you’ve tried on here, we can work it forward from what you have. It would also be helpful if you posted the smallest portion of the models necessary to create the solution. (We don’t need the entire models, just the fields involved.)
products = Product.objects.filter(
).annotate(total=( Sum('warehouseReceivedOf__received_amount', default=0) + Sum('warehouseReceivedOf__cart_amount', default=0) + Sum('warehouseReceivedOf__out_amount', default=0) + Sum('warehouseReceivedOf__shipped_amount', default=0) + Sum('warehouseReceivedOf__disposed_amount', default=0) )
).annotate(inbound=( Sum('warehouseReceivedOf__expected_amount', default=0) - F('total') )
#I can achieve the result I want with python without the orm structure.
#Frankly, I need to convert the python code here to ORM structure.
#The value obtained as a result of the operation here gives me the inbound information.
""" for item in products:
total_diff = 0
for received_qs in item.warehouseReceivedOf.all():
item_expected = received_qs.expected_amount
item_received = received_qs.received_amount
item_cart = received_qs.cart_amount
item_out = received_qs.out_amount
item_shipped = received_qs.shipped_amount
item_dispose = received_qs.disposed_amount
total = item_received + item_cart + item_out + item_shipped + item_dispose
if item_expected > total:
total_diff += item_expected - total
item.inbound = total_diff """
#As I don't know how to use "if item_expected > total" in orm structure, I get a wrong result. As a result of the values satisfying the if condition, I can reach the inbound value.