【发布时间】:2018-08-26 18:47:25
【问题描述】:
我有两个如下所示的查询集:
product_first = Reports.objects.filter(product='product1', type='week', profile__team=team).select_related('profile')
product_second = Reports.objects.filter(product='product2', type='week', profile__team=team).select_related('profile')
其中的每一个都共享obj.profile.user.username,并具有其他类似的属性(例如obj.total) - 我试图最终得到一个基于obj.profile.user.username 的查询集,但obj.total 是@987654326 @。
我正在查询的那个表的示例:
user_id total total_items product type
1 150 15 product1 week
1 180 19 product2 week
【问题讨论】:
-
所以对于每一个
user_id,你想计算总数的总和吗?如果该用户有多个product1s 和/或product2s 怎么办? -
对。幸运的是,该表对于每种类型的产品只有一个条目。那里的数据来自已导入的 CSV,该 CSV 已经汇总了每个产品。我想我可以遍历 CSV 并将总计与设置为“全部”之类的产品类型相结合,但出于几个原因,我想避免这条路线。
标签: django django-models django-queryset