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?
CodePudding user response:
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