Reputation: 26668
Given the following model:
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)
Is there an easy way to query the average number of events that happen per weekday?
Upvotes: 0
Views: 145
Reputation: 55962
django's aggregation queries could help, perhaps something like (untested):
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'))
Upvotes: 1