Django聚合-表达式包含混合类型。您必须设置output_field [英] Django Aggregation - Expression contains mixed types. You must set output_field
问题描述
我正在尝试实现聚合查询,这就是我的代码:
I'm trying to achive an Aggregation Query and that's my code:
TicketGroup.objects.filter(event=event).aggregate(
total_group=Sum(F('total_sold')*F('final_price')))
我在TicketGroup对象中具有 total_sold和 final_price,我要做的就是求和并乘以值,以将所有TicketGroup的总售出总和。
I have 'total_sold' and 'final_price' in TicketGroup object and all what I want to do is sum and multiply values to get the total sold of all TicketGroups together.
我得到的只是这个错误:
All I get is this error:
表达式包含混合类型。您必须设置output_field
Expression contains mixed types. You must set output_field
我在做错什么,因为我将'total_group'称为输出字段?
What I am doing wrong, since I'm calling 'total_group' as my output field?
谢谢!
推荐答案
由 output_field
Django的意思是为 Sum
的结果提供字段类型。
By output_field
Django means to provide field type for the result of the Sum
.
from django.db.models import FloatField, F
total_group=Sum(F('total_sold')*F('final_price'), output_field=FloatField())
应该可以解决问题。
这篇关于Django聚合-表达式包含混合类型。您必须设置output_field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!