如何在Django中减去带注释的值?

时间:2018-12-05 13:44:23

标签: django django-views

我无法在Django视图中减去两个不同查询的注释值。

我尝试过:

x.RenderTransform = new ScaleTransform(1 / zoom.ScaleX, 1 / zoom.ScaleY);

我尝试使用django.db.models.function中的Coalesce,但收到此错误

    qs = Stockdata.objects.filter(User=self.request.user, Company=company_details.pk, Date__gte=selectdatefield_details.Start_Date, Date__lte=selectdatefield_details.End_Date)
    total = qs.annotate(the_sum=Coalesce(Sum('salestock__Quantity'),0)).values('the_sum')
    total2 = qs.annotate(the_sum2=Coalesce(Sum('purchasestock__Quantity_p'),0)).values('the_sum2')
    totalqty = total2 - total

Django中是否有任何函数可以减去值?

有人知道我的代码在做什么错吗?

2 个答案:

答案 0 :(得分:1)

尝试

qs = qs.annotate(
    sales_sum = Coalesce(Sum('salestock__Quantity'),0),
    purchase_sum = Coalesce(Sum('purchasestock__Quantity_p'),0)
)
qs = qs.annotate(
    difference = ExpressionWrapper(F('sales_sum') - F('purchase_sum'), output_field=DecimalField())
) 

答案 1 :(得分:0)

您需要使用aggregate

total = qs.aggregate(the_sum=Coalesce(Sum('salestock__Quantity'),0))
total2 = qs.aggregate(the_sum2=Coalesce(Sum('purchasestock__Quantity_p'),0))

totalqty = total2['the_sum2'] - total['the_sum']