After digging the django request api doc, I did not find a way to make the request through the django ORM system. Cursor is a workaround if your database brand is MySQL:
from django.db import connection, transaction cursor = connection.cursor() cursor.execute(""" select week(time) as `week`, count(*) as `count` from EventOccurrence group by week(time) order by 1;""") myData = dictfetchall(cursor)
This, in my opinion, is the best solution for increasing productivity. But note that this will not miss a week.
EDITED An independent database brand solution using python (less performance)
If you are looking for brand-independent database code, then you should take dates every day and collect them through python. If this is your code, the code might look like this:
#get all weeks: import datetime weeks = set() d7 = datetime.timedelta( days = 7) iterDay = datetime.date(2012,1,1) while iterDay <= datetime.now(): weeks.add( iterDay.isocalendar()[1] ) iterDay += d7 #get all events allEvents = EventOccurrence.objects.value_list( 'time', flat=True ) #aggregate event by week result = dict() for w in weeks: result.setdefault( w ,0) for e in allEvents: result[ e.isocalendar()[1] ] += 1
(Disclaimer: not verified)
danihp
source share