0

在 Django 中,如何使用注解的filter属性Sum()只计算属于唯一模型的属性的 SUM?在下面的代码块中,如何roi_avg以我只能在此伪代码中描述的方式进行过滤:roi_avg = Sum(avg_field, filter= tweets_we_need + Q(tweets__twitter_calls__coin__id==Unique)


avg_field = F('tweets__twitter_calls__roi_plus_year')
tweets_we_need = (Q(tweets__created_at__date__lt=one_year_back) & ~Q(tweets__twitter_calls__roi_plus_year=None))

    users = (
        User.objects        
        .annotate(
            roi_avg = Sum(avg_field, filter= tweets_we_need),
            calls_count = Count('tweets', filter = tweets_we_need),
            unique_coins = Count('tweets__twitter_calls__coin', filter = tweets_we_need, distinct=True)
        )
        .exclude(roi_avg=None)
        .order_by("-roi_avg", "-unique_coins")
    )
4

0 回答 0