【问题标题】:Combine two query sets based on shared field基于共享字段组合两个查询集
【发布时间】: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


【解决方案1】:

我们可以通过在username上进行注释和分组来做到这一点:

from django.db.models import F, Sum

qs = Reports.object.filter(
    product__in=['product1', 'product2'],
    type='week',
    profile__team=team
).values('profile_id').annotate(
    username=F('profile__user__username')
    the_total=Sum('total')
).order_by('profile_id', 'username')

这将产生一个QuerySet 的字典,每个字典包含三个键:'profile_id''username''the_total'。所以对于给定的样本数据,它看起来像:

<QuerySet [{'profile_id': 1, 'username': 'foo', 'the_total': 330}]>

(假设'foo'id=1 用户的username)。

请注意,the_total 将包含所有product1s 和product2s 的totals 的sum。如果没有product2,那么它仍然会显示product1s 的总和。如果有多个product1s,它会总结这些。

【讨论】:

  • 这正是我想要的。现在深入研究文档,因为这看起来很神奇。谢谢!
  • 有没有办法使用totaltotal_items 的聚合值获得平均值?实际上,在annotate() 我有total_items=Sum('total_items')total=Sum('total') 返回预期值,我想做average=total/total_items。我尝试了average=total/total_items 并得到了一个错误,即totaltotal_items 都没有定义,即使我在QuerySet 中都得到了正确的值。
  • 是的,average=Avg('total')(使用 `from django.db.models import Avg)。请注意,您最好不要使用与其中一个字段相同的名称,因为这会导致错误的查询。
  • 这给了我 product_1product_2 totals 的平均值(product_1_total + product_2_total / 2,我正在尝试 product_1_total + product_2_total / product_1_total_items + product_2_total_items。我需要遍历字典的 QuerySet 和为总平均值创建一个新键?
  • 啊,你想要(所有这些产品)的平均值吗?
猜你喜欢
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多