I am trying to group products by DAY, however date_created is a datetime field.
Product.objects.values('date_created') \
.annotate(available=Count('available_quantity'))
returns:
[
{'date_created': datetime.datetime(2012, 4, 14, 13, 3, 6), 'available': 1},
{'date_created': datetime.datetime(2012, 4, 14, 17, 12, 9), 'available': 1},
...
]
I want:
[
{'date_created': datetime.datetime(2012, 4, 14), 'available': 2}, ...
]
edit: database backend MYSQL
In Django 1.4, you could use
.dates('date_created', 'day')
instead of.values()
.Try this code snippet:
This should return:
Similar approach from San4ez's answer, but returning dates as 'YYYY-MM-DD' instead of 'datetime.datetime(YYYY, MM, DD)':
Inspired by this question try this for mysql