Annotate a sum of two fields multiplied

后端 未结 6 956
闹比i
闹比i 2021-01-30 13:28

I have three models, simplified for the example:

class Customer(models.Model):
    email = models.CharField(max_length=128)

class Order(models.Model):
    custo         


        
6条回答
  •  花落未央
    2021-01-30 13:54

    Maybe you don't need this answer now, but if you read the documentation about Sum expression , you need to declare the output_field, like this:

    Customer.objects.filter(something)
                    .annotate(total_spent=Sum(
                        F('order__lineitem__quantity') * 
                        F('order__lineitem__price'),   
                        output_field=models.FloatField()
                    ))
    

提交回复
热议问题