如何在Django中使用带有过滤器的多个注释?

时间:2018-09-27 22:11:59

标签: python django django-models

我使用下面的代码来获取结果,但是对于total_deal和total_coupons总是得到相同的输出。我不知道这段代码中发生了什么。

total_coupons = Count('coupon', filter=Q(coupon__is_sale__lte=0))
total_deals = Count('coupon', filter=Q(coupon__is_sale__gt=0))

queryset = coupon.objects.values('store_id','store_name','store_site','affiliate_url').annotate(total_deals=total_deals, total_coupons=total_coupons)

那么如何编写查询以使结果低于我?

store_id  store_name store_site affiliate_url total_coupons total_deals
1         abc        abc.com    anything       5             3         (totsl is 8)

反正还有高于输出的水平吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试一下,annotate(total_deals=total_deals).annotate(total_coupons=total_coupons)而不是.annotate(total_deals=total_deals, total_coupons=total_coupons)

queryset = coupon.objects.values('store_id','store_name','store_site','affiliate_url').annotate(total_deals=total_deals).annotate(total_coupons=total_coupons)