【发布时间】:2014-07-08 04:15:21
【问题描述】:
我想在模型中的列上使用聚合函数。
以下是我的模型:-
class ShipmentWeightMapping(models.Model):
weight = models.CharField(max_length = 255)
status = models.CharField(max_length = 255)
time = models.DateTimeField( auto_now_add = True)
shipment_id = models.ForeignKey('Shipment', related_name = 'weights')
这是我的聚合查询:-
shipment_weight_obj = ShipmentWeightMapping.objects.filter(shipment_id__in = shipment_id_list).aggregate(Avg('weight'),Max('weight'),Min('weight'),Sum('weight'))
total_weight = shipment_weight_obj['weight__sum']
max_weight = shipment_weight_obj['weight__max']
min_weight = shipment_weight_obj['weight__min']
avg_weight = shipment_weight_obj['weight__avg']
上面的代码以 MySQL 作为 DB 运行,但与 postgres 一起使用时返回错误:-
LINE 1: SELECT SUM("data_shipmentweightmapping"."weight") AS "weight...
[Tue Jul 08 04:05:38 2014] [error] ^
[Tue Jul 08 04:05:38 2014] [error] HINT: No function matches the given name and argument types. You might need to add explicit type casts.
这是怎么回事?我知道 weight 是一个 char 字段,我正在添加一个 char 字段,我认为它在 postgres 中是不允许的,但是如何更改我的查询以使其正常工作?另外,为什么它在 MySql 中而不是在 postgres 中有效
【问题讨论】:
标签: mysql django postgresql aggregate-functions