Django平均值 - 每个工作日的平均事件数

时间:2013-06-18 22:27:54

标签: django

鉴于以下模型:

DAY_OF_WEEK_CHOICES = (
    ('monday', 'Monday'),
    ('tuesday', 'Tuesday'),
    ('wednesday', 'Wednesday'),
    ('thursday', 'Thursday'),
    ('friday', 'Friday'),
    ('saturday', 'Saturday'),
)

class EventForDay(models.Model):
    event = models.ForeignKey(Event, db_index=True)
    day_of_week = models.CharField(max_length=15, choices=DAY_OF_WEEK_CHOICES,
        db_index=True)
    day = models.DateField(db_index=True)

是否有一种简单的方法可以查询每个工作日发生的平均事件数量?

1 个答案:

答案 0 :(得分:1)

django的aggregation查询可能有所帮助,或许类似(未经测试):

from django.db.models import Avg
WEEKDAYS = ('monday', 'tuesday', 'wednesday', 'thursday', 'friday')
EventForDay.objects.filter(day_of_week__in=WEEKDAYS).annotate(Avg('day_of_week'))