Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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 Python-当月和过去6个月的查询计数_Python_Django_Count - Fatal编程技术网

Django Python-当月和过去6个月的查询计数

Django Python-当月和过去6个月的查询计数,python,django,count,Python,Django,Count,我将Django1.10.5与python3.6一起使用 我有以下模型类: class PublishedRecordedActivity(models.Model): published_details = models.ForeignKey(PublishedDetails, null=True, blank=True, on_delete=models.CASCADE) timestamp_added = models.DateTimeField(auto_now_add=

我将Django
1.10.5
与python
3.6
一起使用

我有以下模型类:

class PublishedRecordedActivity(models.Model):
    published_details = models.ForeignKey(PublishedDetails, null=True, blank=True, on_delete=models.CASCADE)
    timestamp_added = models.DateTimeField(auto_now_add=True)
    activity_type = models.IntegerField(null=False, blank=False, default=1)
我想计算当前月份以及过去6个月内每种活动类型(1、2、3或4)的记录数

例如,整个当月(2019年4月)的计数

一个月前(2019年3月整月)的计数

两个月前(2019年2月整月)的计数等

我可以为计数编写查询,但我不确定如何为整个月添加过滤器

我的问题是:

test_count = PublishedRecordedActivity.objects.filter(activity_type=1).count

您可以使用Django的
annotate
truncmount
Count
数据库功能获得每月聚合值。下面给出了使用示例

i、 e

这将为您提供特定活动类型的每月汇总计数。您可以在添加的
时间戳
字段中添加其他过滤器应用时间范围

如果您只想在过去6个月内进行汇总,请遵循以下步骤

from datetime import date, timedelta
current_date = date.today()
months_ago = 6
six_month_previous_date = current_date - timedelta(days=(months_ago * 365 / 12))

aggregated = PublishedRecordedActivity.objects.filter(activity_type=1, timestamp_added__gte=six_month_previous_date).annotate(month=TruncMonth('timestamp_added').values('month').annotate(sum_by_month=Count('id'))
months\u ago
替换为所需的月份合计


这对你有帮助。如果需要进一步的帮助,请首先在下面添加注释,找出要筛选的月份。为此,请使用软件包中的函数

In [33]: from datetime import datetime                                                                                                                                                                             

In [34]: from dateutil.relativedelta import relativedelta                                                                                                                                                          

In [35]: months_before = 6                                                                                                                                                                                         

In [36]: now = datetime.utcnow()                                                                                                                                                                                   

In [37]: now                                                                                                                                                                                                       
Out[37]: datetime.datetime(2019, 4, 8, 5, 6, 42, 300424)

In [38]: from_datetime = now - relativedelta(months=months_before)                                                                                                                                                 

In [39]: from_datetime                                                                                                                                                                                             
Out[39]: datetime.datetime(2018, 10, 8, 5, 6, 42, 300424)

In [40]: modified_from_datetime = from_datetime.replace(day=1, hour=0, minute=0, second=0, microsecond=0)                                                                                                          

In [41]: modified_from_datetime                                                                                                                                                                                    
Out[41]: datetime.datetime(2018, 10, 1, 0, 0)
然后使用过滤器中的
modified\u from\u datetime
变量


更新-1 将“按功能分组”用作

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

aggregated = PublishedRecordedActivity.objects.filter(
    activity_type=1).annotate(month=TruncMonth('timestamp_added')).values('month').annotate(sum_by_month=Count('month'))

这将以月为单位返回计数。i、 e 2019年1月,计数:2;2019年2月,计数:5。。。。2019年12月,计数:6…根据您的建议,我如何将过滤器应用于4个月前添加的
时间戳?我已经更新了我的答案并进行了调查。它返回以前六个月的聚合值,每个月按特定的
活动类型分组。这将返回每个月的计数。谢谢,但您的回答提供了过去六个月的总数。我正在寻找过去6个月内每个月的计数。请查看我的
UPDATE-1
部分非常感谢!您提供的update-1解决了问题!
PublishedRecordedActivity.objects.filter(activity_type=1, timestamp_added__gte=modified_from_datetime)
from datetime import datetime
from dateutil.relativedelta import relativedelta

months_before = 6
now = datetime.utcnow()
from_datetime = now - relativedelta(months=months_before)
modified_from_datetime = from_datetime.replace(day=1, hour=0, minute=0, second=0, microsecond=0)

PublishedRecordedActivity.objects.filter(activity_type=1, timestamp_added__gte=modified_from_datetime)
from django.db.models.functions import TruncMonth
from django.db.models.aggregates import Count

aggregated = PublishedRecordedActivity.objects.filter(
    activity_type=1).annotate(month=TruncMonth('timestamp_added')).values('month').annotate(sum_by_month=Count('month'))