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

103
Views
Incremental totals in Django Model

I have a table as below: Example Table

and I have a model as

class Hours(models.Model):
      name = model.Charfield(max_length =200)
      hours = DecimalField(max_digits = 6, decimal_places=2)

     def total_hours(self):
        queryset = Hours.objects.aggregate(total_hours=models.Sum('hours'))
        return queryset['total_hours']

now when I run the code I get all records have the to 37 instead of them having a running total.

where did I miss a step?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Whenever you call total_hours its getting the sum of hours over all Hours objects. You'd need something like:

Hours.objects.filter(id__lte=self.id).aggregate(total_hours=models.Sum('hours')) 

for a running total

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!