It seems to me that the easiest way is to add a third column with the difference and update it every time the object is modified like this:
class Timer(models.Model)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
diff_time = models.DateTimeField()
def __init__(self, *args, **kwargs):
super(Timer, self).__init__(*args, **kwargs)
self.diff_time = self.end_time - self.start_time
def save(self, *args, **kwargs):
self.diff_time = self.end_time - self.start_time
super(Timer, self).save(*args, **kwargs)
I tried to look for a solution with annotate, but there is no support for aggregate functions in either mysql nor postgres. Even if there where, seem like nice trade off an extra column for not having to calculate the diff for each row on each query!
Oh, and you can just retrieve what you want like this:
Timer.objects.values('id', 'diff_time')