I have a class which contains a date as well as some other fields.
What I'm trying to figure out is a query which will return me each of the items, grouped by the date.
So, given the following class:-
class Item(models.Model):
item_date = models.DateField()
<other fields>
I'd like to get something like this:-
[
{
'item_date': datetime.date(2018, 9, 12),
'items': <Queryset [<Item: item1>, <Item: item17> ...]
}, {
'item_date': datetime.date(2018, 9, 13),
'items': <Queryset [<Item: item2>, <Item: item33> ...]
}, {
'item_date': datetime.date(2018, 9, 14),
'items': <Queryset [<Item: item34>, <Item: item37> ...]
} ...
]
I'm pretty sure I need some sort of annotation but I'm not 100% sure how to structure it.
The closest I've got is:-
Item.objects.values('item_date').annotate(Count('id')).order_by('item_date')
but that just gives me the counts of how many Items are on each date - not the actual Items (obviously!).
What do I need instead of that Count? Is this even possible?!