Count by date

4 views
Skip to first unread message

Simon

unread,
Nov 20, 2009, 1:59:07 PM11/20/09
to Django users
Hi all,

I am sure this must be a real noob question as it's so easy to do in
SQL.
I am trying to replicate something like the following in Django:

SELECT DATE(created) AS created_day, COUNT(id) AS NumberOf FROM
my_table GROUP BY created_day

Effectively, all I want to do is a count grouped by a specific day,
week or month.
I assumed I could use something like

MyTable.objects.annotate(Count('created'))

but I can't find a way to format the "created" date such that it
ignores the less significant elements, such as the hours and minutes
and only counts entries based on the unit I want.

Anyone any ideas how to do this?
Thanks

Martin Ostrovsky

unread,
Nov 23, 2009, 8:52:08 AM11/23/09
to Django users
You can limit your search by year, month or day.

See http://docs.djangoproject.com/en/dev/ref/models/querysets/#year

philadams

unread,
Dec 12, 2009, 1:09:30 PM12/12/09
to Django users
i believe the way filters work (what martin referenced) is that you
have to explicitly say which weekday you care about.

what the op is looking for is a way to group by week day. ideally to
write something like MyTable.objects.values(created__week_day).annotate
(Count('id'))

however, this doesn't work as created__week_day is not a valid
argument to values. any ideas how to actually do this?

On Nov 23, 8:52 am, Martin Ostrovsky <martin.ostrov...@gmail.com>
wrote:
> You can limit your search by year, month or day.
>
> Seehttp://docs.djangoproject.com/en/dev/ref/models/querysets/#year
Reply all
Reply to author
Forward
0 new messages