0

I'm using Django 2.0

I am preparing data to show on a graph in template. I want to fetch number of records per day.

This is what I'm doing

qs = self.get_queryset().\
        extra({'date_created': "date(created)"}).\
        values('date_created').\
        annotate(item_count=Count('id'))

but, the output given is

[
    {'date_created': datetime.date(2018, 5, 24), 'item_count': 1},
    {'date_created': datetime.date(2018, 5, 24), 'item_count': 1}, 
    {'date_created': datetime.date(2018, 5, 24), 'item_count': 1}, 
    {'date_created': datetime.date(2018, 5, 24), 'item_count': 1}, 
    {'date_created': datetime.date(2018, 5, 24), 'item_count': 1}, 
    {'date_created': datetime.date(2018, 5, 24), 'item_count': 1}, 
    {'date_created': datetime.date(2018, 5, 24), 'item_count': 1}
]

Here data is not grouped and same date is returning repeatedly with count as 1

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • There is similar question to your question, You can use this [answer](https://stackoverflow.com/questions/10154227/django-orm-group-by-day?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – Minuddin Ahmed Rana Jun 06 '18 at 07:08
  • I tried that, still same output – Anuj TBE Jun 06 '18 at 07:18

1 Answers1

0

Try using TruncDate function.

See that answer

afilardo
  • 527
  • 2
  • 16