Django在created_at上注释

时间:2017-03-06 04:40:05

标签: python django

我想了解每天在Django中创建的记录数,但我无法使其正常工作。到目前为止,我已经得到了这个:

data = Reservation.objects\
        .order_by('created_at')\
        .extra({'dp': "date(created_at)"}) \
        .values('dp').annotate(count=Count('id'))

返回

<QuerySet [{'count': 1, 'dp': datetime.date(2017, 2, 28)}, {'count': 1, 'dp': datetime.date(2017, 2, 28)}, {'count': 1, 'dp': datetime.date(2017, 2, 28)}, {'count': 1, 'dp': datetime.date(2017, 2, 28)}, {'count': 1, 'dp': datetime.date(2017, 3, 1)}, {'count': 1, 'dp': datetime.date(2017, 3, 1)}, {'count': 1, 'dp': datetime.date(2017, 3, 1)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, {'count': 1, 'dp': datetime.date(2017, 3, 2)}, '...(remaining elements truncated)...']>

我如何统计dp条记录,以便获得:

<QuerySet [{'count': 4, 'dp': datetime.date(2017, 2, 28)}{'count': 3, 'dp': datetime.date(2017, 3, 1)},..

2 个答案:

答案 0 :(得分:3)

这应该是最后一个:

.order_by('dp')

答案 1 :(得分:2)

您对模型预订有任何元排序吗?否则查询应该可以正常工作