Django,从注释的最大值中选择一个最大值(从几个字段中选择最大值)

我有一个关于
django注释方法的问题:

是否可以计算这3个特定折扣的“maximum_discount”(并按此“maximum_discount”排序)?

Product.objects\
   .annotate(
        product_discount=Max('discounts__amount'),
        category_discount=Max('category__discounts__amount'),
        brand_discount=Max('brand__discounts__amount')
    )

最佳答案 尝试这样的事情:

max_discount = max(value for key, value in Product.objects\
   .annotate(  # maybe 'aggregate'?
        product_discount=Max('discounts__amount'),
        category_discount=Max('category__discounts__amount'),
        brand_discount=Max('brand__discounts__amount')
    )
)
点赞