I need to find out average age of items (groupped by various criteria, but it does not seem to be the issue). However I fail to find how to effectively create aggregation over DateTimeField using Django (on top of MySQL).
Pure Item.objects.all.aggregate(Avg('created'))
seems to produce absolutely bogus values (eg. 20081988007238.133) and using Item.objects.all.extra(...)
I have not found a way how to aggregate.
Of course I can manually create SQL query (something like SELECT AVG(UNIX_TIMESTAMP(created)) FROM items_item
), but I'd prefer to avoid using MySQL specific code in the application.
Just for the reference, sample model I use for testing:
class Item(models.Model):
name = models.CharField(max_length = 255)
created = models.DateTimeField()