Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

523
Views
Summing columns in Django with related fields

I'm trying to do some aggregation on some of my Django columns, but I'm getting an error I can't figure out. Here are my models (the relevant stuff anyways):

class Fillup(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    date = models.DateField(default=date.today)
    trip_distance = models.FloatField(validators=[MinValueValidator(0.0)])
    car = models.ForeignKey('Car',on_delete=models.CASCADE, related_name='fillups')


class Car(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    name = models.CharField(max_length=25)

So each Fillup object is essentially a tank of gas in a car, and what I want to do is get the total of the trip_distance column for each car. I've tried several things from various other StackOverFlow pages, but nothing seems to work.

My first attempt was this adding this field to the Car Model:

@property
def distance_driven(self):
    return self.fillups.aggregate(sum('trip__distance'))

But I just get the following error:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

I also tried adding this to my serializer, but got the same error:

distance_driven = serializers.SerializerMethodField()

def get_distance_driven(self, ob):
    return ob.fillups.all().aggregate(sum('trip_distance'))['trip__distance']

Is there something I'm missing here? I don't understand why I'm getting that error when trying to sum a FloatField column.

Any help would be greatly appreciated!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In order to aggregate, you use a Sum object [Django-doc], not the Python builtin sum.

You can obtain the sum of a single object with:

from django.db.models import Sum

return self.fillups.aggregate(total=Sum('trip_distance'))['total']

If you have to do this for multiple records, it is better to .annotate(…) [Django-doc]:

from django.db.models import Sum

Car.objects.annotate(
    total_distance=Sum('fillups__trip_distance')
)

The Car objects that arise from this queryset will have an extra attribute .total_distance with the sum of the related Fillup trip_distances.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!