I am trying to calculate the difference between consecutive numeric values in the odometer_reading field of my model.
My Models.py has fields like below:
class Refuel(models.Model):
vehicle = models.ForeignKey(Vehicle, blank=True, null=True, on_delete=models.SET_NULL)
gaz_station = models.ForeignKey(
GazStation, related_name=_("Station"), blank=True, null=True, on_delete=models.SET_NULL
)
odometer_reading = models.PositiveIntegerField(_("Compteur KM"), blank=True, null=True)
snitch = models.PositiveIntegerField(_("Mouchard KM"), blank=True, null=True)
fuel_quantity = models.DecimalField(_("Quantitée en Litres"), max_digits=5, decimal_places=1)
fuel_unit_price = models.DecimalField(_("Prix en DH"), max_digits=6, decimal_places=2)
note = models.CharField(_("Remarque"), max_length=255, blank=True, null=True)
created_at = models.DateTimeField(_("Created at"), auto_now_add=True, editable=False)
updated_at = models.DateTimeField(_("Updated at"), auto_now=True)
is_active = models.BooleanField(default=True)
@property
def total_price(self):
total_price = self.fuel_quantity * self.fuel_unit_price
return total_price
class Meta:
ordering = ["gaz_station", "-created_at"]
def __str__(self):
return self.vehicle.serie
I want to use a CBV to get the distance between every two refuel for the same vehicle so I can calculate fuel consumption per km.
is there a way to do it?
EDITED:
I want to return with every refuel the fuel consumption per km using the precedent refuel.