【发布时间】:2021-06-13 08:31:01
【问题描述】:
我有一个 django 模型如下:
class Order(models.Model):
cash=models.DecimalField(max_digits=11,decimal_places=2,default=0)
balance=models.DecimalField(max_digits=11,decimal_places=2,default=0)
current_ac=models.DecimalField(max_digits=11,decimal_places=2,default=0)
added_by = models.ForeignKey(User)
可以有多个订单,多个用户可以创建订单。
如何获取特定用户每列的所有订单总和,例如
ord=Order.objects.filter(added_by.id=1).sum()
SQL 等价物类似于
Select sum(cash), sum (balance), sum(current_ac) from Orders where added_by = 1
【问题讨论】:
-
您要对哪一列求和?
current_ac或cash? -
所有这些。假设有 2 个订单订单 1 和 2,现金 3 和 5,余额 1 和 2,当前 ac 3 和 4。我希望输出是 Order 对象,其中现金 8,余额 3 和当前 ac 7 作为值。跨度>
-
查看编辑后的答案。
标签: python django django-models