I have a Django model where I'd like to calculate a duration field based on the inputted data from two DateTime fields and store the difference between them in hours: minutes (I'm not even sure the duration field is the right choice here.
This is what I got so far.
class CarEventsData(models.Model):
car = models.ForeignKey(Car, on_delete=models.CASCADE, related_name="events_of_cow")
out = models.DateTimeField(null=True, help_text="When the cow went OUT TO Pasture.")
_in = models.DateTimeField(null=True, help_text="When the cow got back IN FROM Pasture.")
duration = models.DurationField()
def save(self, *args, **kwargs):
"""
Should set duration field to total time between out and in data.
"""
self.duration = self.out - self._in
super(CarEventsData, self).save(*args, **kwargs)