How to write a Django QuerySet the properly computes average of DateTimeField with grouping?
- 阅读更多 关于 How to write a Django QuerySet the properly computes average of DateTimeField with grouping?
问题 Here is my Django model: class MyModel(models.Model): a = IntegerField() b = DateTimeField() Here is the QuerySet I execute on this model to find the count, min, max and average b s for each value of a : >>> from django.db.models import Count, Max, Min, Avg >>> MyModel.objects.extra( ... select={'avg': 'AVG(UNIX_TIMESTAMP(b))'} ... ).values('a').annotate( ... count=Count('b'), ... min=Min('b'), ... max=Max('b'), ... ) Here is the result of the QuerySet above: [ {'a': 1, 'count': 5, 'min':