I have two cases:
Case 1:
qs = Stockdata.objects.annotate(sales_sum=Coalesce(Sum('salestock__quantity')))
qs2 = Stockdata.objects.annotate(purchase_sum=Coalesce(Sum('purchasestock__quantity_p')))
Case 2:
        qs = Stockdata.objects.annotate(
           sales_sum = Coalesce(Sum('salestock__quantity'),0),
           purchase_sum = Coalesce(Sum('purchasestock__quantity_p'),0))
The result which is coming in Case 1 is absolutely perfect but when I am trying Case 2 the result is coming multiplying the no of entries I made..
Can anyone tell me what is the reason for this and can give a perfect solution to rectify the errors because the 2nd Case is what I need to use in my project...
Thank you
Edit
I have tried this according to the response:
        qs = Stockdata.objects.annotate(
        sales_sum = Subquery(
            Stock_total_sales.objects.filter(
                sales = OuterRef('pk')
                ).values(
                    'sales'
                ).annotate(
                    the_sum = Sum('quantity')
                ).values('the_sum')
            ),
        purchase_sum = Coalesce(Sum('purchasestock__quantity_p'),0)
But having a bit problem of using "the_sum" in further subtraction of annotated values..
I had done this previously:
        qs1 = qs.annotate(
        difference = ExpressionWrapper(F('purchase_sum') - F('sales_sum'), output_field=DecimalField()),
    ) 
Anyone any suggestion what should I use instead of sales_sum.
Thank you
