0

我有这个查询集函数来计算字段created,但我想按天计算,而不是现在的情况,因为我想用这些数据制作图表。

           queryset = Model.objects.filter(
            user=user
        ).values("created").annotate(
            Count("created")).order_by("created")

结果:

 [{"created__count": 1, "created": "2013-10-01T09:05:50Z"}, {"created__count": 1, "created": "2013-10-01T09:05:53Z"}, {"created__count": 1, "created": "2013-10-01T09:05:56Z"}, {"created__count": 1, "created": "2013-10-01T09:26:37Z"}, {"created__count": 1, "created": "2013-10-01T09:26:41Z"}, {"created__count": 1, "created": "2013-10-01T09:27:10Z"}, {"created__count": 1, "created": "2013-10-01T09:27:15Z"}, {"created__count": 1, "created": "2013-10-01T10:12:40Z"}, {"created__count": 1, "created": "2013-10-01T10:12:46Z"}, {"created__count": 1, "created": "2013-10-01T10:12:53Z"}]

这有一些方法可以按天计算,例如 11/11:Count 20、12/11:Count 22。

我应该在从查询集中获得这些数据后计算它吗?

4

1 回答 1

1

你应该试试Count('created__day')。至少created__day应该在查询的过滤部分工作,所以直观地说,它也应该与Count.

编辑:

...不,它不起作用,但您可能会在Count number of records by date in Django中找到解决方案。

所以基本上你必须.extra(...)结合使用一些(可能是非标准的)SQL 函数,这些函数只提取日期时间的一部分(例如 MySQL DATE()),然后将结果字段用于Count.

于 2013-10-02T11:33:56.627 回答