I want to use aggregate functions on a column in my model.
Following is my model:-
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')
Here is my aggregation query:-
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']
The above code runs with MySQL as DB but returns an error when used with postgres it returns an error:-
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.
How come is this happening? I know that weight is a char
field and I'm adding a char
field, which I suppose is not allowed in postgres, but how can alter my query so that it works ? Also , why it works in MySql and not in postgres