Annotate a sum of two fields multiplied

后端 未结 6 848
闹比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条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 13:53

    Similar to: https://stackoverflow.com/a/19888120/1344647

    from django.db.models import Sum
    
    q = Task.objects.filter(your-filter-here).annotate(total=Sum('progress', field="progress*estimated_days"))
    

    Edited: Thanks to @Max, using annotate instead aggregated.

提交回复
热议问题