Let us say I have a very simple model like this one:
class Test(models.Model):
category = models.CharField(unique=True)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
Basically, I want to use Django's ORM API to get the average duration per category. The SQL query will look something like this:
SELECT category, AVG(end_time - start_time) AS avg_duration
FROM test_table
GROUP BY category
I tried using the following code, but according to the docs, F() expressions in .annotate() is only available in Djano 1.8.
Test.objects.values('category', 'start_time', 'end_time').annotate(avg_duration=Avg(F(end_time) - F(start_time))
I also tried using .extra() like this, but I get a FieldError.
Test.objects.extra(select={'duration':'end_time - start_time'}).values('category', 'duration').annotate(avg_duration=Avg('duration'))
From the looks of things, the 2nd attempt suggests that the annotate function cannot resolve column aliases. Is this really the case, or am I missing something?
Furthermore, short of creating an additional column that stores derived information (duration per entry), using Django 1.8, and/or using raw SQL, what other alternatives can you guys recommend? Any help will very much be appreciated. Thanks!