Group by object's field and return list of object or object's id for each group

8 views
Skip to first unread message

Aamu Padi

unread,
May 7, 2018, 10:14:36 AM5/7/18
to django...@googlegroups.com
class Ticket(models.Model):
            ...
    booked_at = models.DateTimeField(default=timezone.now)
    bought = models.BooleanField(default=False)

I would like to group tickets by booked day to get list of ticket or ticket's id for each day. Something like this:

[
{
'day': datetime.datetime(2018, 5, 6, 0, 0, ...>), 
'ticket_list': [1, 2, 3, 4],
}, 
{
'day': datetime.datetime(2018, 5, 7, 0, 0, ...>),
'ticket_list': [5, 6, 7, 8, 9],
}
]

I could group tickets by day this way and count total tickets per day,

Ticket.objects.filter(bought=True).annotate(day=TruncDay('booked_at')).values('day').annotate(c=Count('id')).order_by()

But I cannot figure out how to group by day and return ticket objects for that day. Could you please help me solve this.

Thank you
Reply all
Reply to author
Forward
0 new messages