most_viewed_videos = Video.objects.filter(
videowatch__created__month=today.month,
viewing__status="D"
).annotate(
count=Count("videowatch"),
viewing_count=Sum("viewing")
).values("count", "viewing_count").order_by(
"-viewing_count",
"-count"
)
嘿,我有这段代码片段,我的问题是如何将count
和viewing_count
加在一起得到一个结果。
我试过了,但我找不到任何有用的东西。
编辑: 什么对我有用的解决方案
most_viewed_videos = Video.objects.filter( videowatch__created__month=today.month, viewing__status="D" ).extra( select={'total': 'COUNT("videowatch") + SUM(CAST("viewing" as CHAR))'} ).values("total").order_by("-total")
答案 0 :(得分:1)
您可以使用.extra queryset方法来实现:
most_viewed_videos = Video.objects.filter(
videowatch__created__month=today.month,
viewing__status="D"
).annotate(
count=Count("videowatch"),
viewing_count=Sum("viewing")
.extra(
select={'total': 'COUNT(videowatch)+SUM(viewing)'}
)
).values("count", "viewing_count").order_by(
"-viewing_count",
"-count"
)
我还没有测试过它。我遇到过类似的问题“对以前注释的值的注释”。似乎这是不可能的,我过去打开过ticket但它已经关闭了。