Get prevoius day total daily km in models property method

Hello Developers i am stuck in a problem , i want to get sum of daily km on models @property. so i can easily render data in templates.

class Log(models.Model):
vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE)
date_time = models.DateTimeField(default=timezone.now, blank=True, null=True)

@property
def daily_km(self):
    return Logsheet.objects.filter(log=self).aggregate(Sum('total_km'))['total_km__sum']

and related_name models is below

class Logsheet(models.Model):
log = models.ForeignKey(Log, on_delete=models.CASCADE, related_name="logsheets")
driver = models.ForeignKey(Driver, on_delete=models.CASCADE, blank=True, null=True)
trip = models.IntegerField(blank=False, null=False)
distance_from = models.FloatField(blank=True, null=True, )
distance_to = models.FloatField(blank=True, null=True, )
time_from = models.TimeField(blank=False, null=False, default=timezone.now)
time_to = models.TimeField(blank=False, null=False, default=timezone.now)
source = models.CharField(max_length=100, blank=True, null=True)
destination = models.CharField(max_length=100, blank=True, null=True)
total_km = models.FloatField(blank=True, null=True,)

and while i am successfully get sum of daily km on that day,i case of previous day sum of daily km is not calcutaed by me help me out. Thank You!

Back to Top