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

2024-04-25 06:44:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我将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=True)
    activity_type = models.IntegerField(null=False, blank=False, default=1)

我要计算当前月份和过去6个月中每个活动类型(1、2、3或4)的记录数。在

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

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

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

我可以编写count的查询,但是我不确定如何为每个月添加过滤器。在

以下是我的疑问:

^{pr2}$

Tags: 模型falsetruemodelonmodelsdetailsnull
2条回答

首先,找出要过滤的月份。为此,请使用dateutil包中的^{}函数

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_from_datetime变量

^{pr2}$

完整代码段

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)

更新-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'))

您可以使用Django的annotateTruncMonthCount数据库函数来获得每月的聚合值。下面给出了使用示例

from django.db.models.functions import TruncMonth

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

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

如果你只想在过去的6个月内进行汇总,那就做下面的事情。在

^{pr2}$

Replace months_ago with months aggregate you want

这对你有帮助。如果需要进一步的帮助,请在下面发表评论

相关问题 更多 >