Clash
Clash

Reputation: 5025

Django, how to group by day

I've tried the following with no success:

Match.objects.filter(sendDate__gte=dateToStats).values("sendDate__day").annotate(perDay=Count("id")).order_by()

Fails with:

Cannot resolve keyword 'sendDate__day' into field.

Where sendDate is a DateTime field, and dateToStats is just a certain date I'm filtering. I'm interested in having the number of matches per day (based on sendDate).

Thanks a lot guys!

Upvotes: 1

Views: 1695

Answers (1)

Manoj Govindan
Manoj Govindan

Reputation: 74795

I don't think the __day mechanism used with filters (field__day) works with values. If your database has a function to extract the day from your date field you can do something like the snippet shown below.

Match.objects.filter(sendDate__gte=dateToStats).extra(
    select = {"sendDate__day": "extract (day from sendDate)"})

Extract (day from sendDate) is specific to Postgresql. You will have to replace it with your database's equivalent.

Upvotes: 2

Related Questions