Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django:按月计算对象数_Django - Fatal编程技术网

Django:按月计算对象数

Django:按月计算对象数,django,Django,我研究了Django docu并尝试了许多示例,但它仍然不起作用,尽管它实际上应该非常简单 这是我的模型: class Dummy(models.Model): timestamp = models.DateTimeField() 我有一些示例对象,可以通过shell列出,如下所示: % python manage.py shell Python 3.7.7 (default, Mar 10 2020, 15:43:33) [Clang 11.0.0 (clang-1100.0.33

我研究了Django docu并尝试了许多示例,但它仍然不起作用,尽管它实际上应该非常简单

这是我的模型:

class Dummy(models.Model):
    timestamp = models.DateTimeField()
我有一些示例对象,可以通过shell列出,如下所示:

% python manage.py shell
Python 3.7.7 (default, Mar 10 2020, 15:43:33) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from conv.models import Dummy
>>> dummy = Dummy.objects.order_by('timestamp')
>>> for d in dummy:
...     print(d.id, d.timestamp)

6 2019-05-01 07:00:06+00:00
4 2020-04-01 22:00:00+00:00
5 2020-04-15 04:00:00+00:00
1 2020-05-17 09:42:00+00:00
2 2020-05-18 09:42:15+00:00
3 2020-05-19 09:42:21+00:00
>>>
>>> Dummy.objects.filter(timestamp__year='2020', timestamp__month='5').count()
3
现在我想按年/月计算对象。仅按年份过滤有效:

>>> Dummy.objects.filter(timestamp__year='2020').count()
5
但按月份过滤不起作用:

>>> Dummy.objects.filter(timestamp__month='5').count()
0
实际上结果应该是4。有什么问题吗?

您可以使用获取字典查询集,其中
month
是一个
datetime
对象,总是指定月份的第一个,并且
count
将包含给定月份的项数:

from django.db.models import Count
from django.db.models.functions import TruncMonth

Dummy.objects.values(
    month=TruncMonth('timestamp')
).annotate(
    count=Count('pk')
).order_by('month')

我使用的MySQL数据库有一些问题,与之非常相似。当我切换回SQLite时,我得到了正确的结果:

>>> Dummy.objects.filter(timestamp__month='5').count()
4
现在,我还可以像这样对年和月进行组合过滤:

% python manage.py shell
Python 3.7.7 (default, Mar 10 2020, 15:43:33) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from conv.models import Dummy
>>> dummy = Dummy.objects.order_by('timestamp')
>>> for d in dummy:
...     print(d.id, d.timestamp)

6 2019-05-01 07:00:06+00:00
4 2020-04-01 22:00:00+00:00
5 2020-04-15 04:00:00+00:00
1 2020-05-17 09:42:00+00:00
2 2020-05-18 09:42:15+00:00
3 2020-05-19 09:42:21+00:00
>>>
>>> Dummy.objects.filter(timestamp__year='2020', timestamp__month='5').count()
3

幸亏我现在看得更清楚了,也学到了很多关于TruncMonth和ExtractMonth的知识,这似乎也是非常有用的工具。

好的,字典看起来是这样的:
{'month':datetime.datetime(2019,5,1,0,0,tzinfo=)
你说:“…count将包含给定月份的项数”,但没有“count”但是,所以我仍然必须每月计算项目,对吗?@RalfZosel:对不起,答案中有一个打字错误,隐藏了
count=…
部分,请参见编辑。现在
count
在字典中,这很好,但实际上我的意图是让它“独立”。顺便说一下:我在问题
Dummy.objects.filter(timestamp\uu month='5').count()中提到的过滤示例确实提供了4–我不知道我之前做错了什么。(我将在问题中纠正这一点,好吗?)当然,TruncMonth提供的年和月的组合要好得多。现在的问题是:我该如何过滤?类似于
Dummy.objects.values(month=truncmount('timestamp')).filter(month='2020-05-01 00:00').count()
提供0(而不是3)。@RalfZosel:如果使用
extractmount
,会怎么样?请参阅edit.Works很棒,可以与按年份过滤结合使用:
Dummy.objects.values(year=ExtractYear('timestamp'),month=ExtractMonth('timestamp'))。filter(year=2020,month=5)。count()
提供3个,这很好。实际上,这与我最初想到的解决方案非常相似:
Dummy.objects.filter(timestamp\uu year='2020',timestamp\uu month='5').count()
。但是,我从中学到了很多,非常感谢你的帮助!我最初的问题显然与mysql和tz有关,如下所述:(我同时切换到了SQLite)。