我是Django的新手。我试图使此查询返回按组计数。但这不会对数据进行分组。
notification = AppointmentNotificationGroupAppointment.objects.filter(receiver__notification_group__group=group).values('receiver__notification_group__group', 'sender__status__name').annotate(pcount=Count('sender__status__name', distinct=True))
它返回:
{'receiver__notification_group__group': '841536_123856', 'sender__status__name': 'Pending', 'pcount': 1},
{'receiver__notification_group__group': '841536_123856', 'sender__status__name': 'Pending', 'pcount': 1},
{'receiver__notification_group__group': '841536_123856', 'sender__status__name': 'Confirmed', 'pcount': 1},
{'receiver__notification_group__group': '841536_123856', 'sender__status__name': 'Confirmed', 'pcount': 1}
我在做什么错?我希望它返回不同的记录,并按组进行计数
答案 0 :(得分:2)
您也需要致电 order_by(...)
AppointmentNotificationGroupAppointment.objects.filter(receiver__notification_group__group=group).values(
'receiver__notification_group__group',
'sender__status__name').annotate(pcount=Count('sender__status__name', distinct=True)
).order_by('receiver__notification_group__group')