0

给定以下模型:

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)

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

4

1 回答 1

1

django 的聚合查询可能会有所帮助,可能类似于(未经测试):

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'))
于 2013-06-18T22:49:47.340 回答