繁体   English   中英

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

[英]Django Python - query count for the month and the past 6 months

我正在将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

最初,找出要过滤的月份。 为此,请使用dateutil包中的relativedelta函数

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变量与您的过滤器gte

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)

UPDATE-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的annotateTruncMonth and Count数据库函数获取每月的汇总值。 使用示例如下

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个月的数据,则可以进行后续操作。

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_ago的月份总数替换months_ago

这将对您有帮助。 如果需要进一步帮助,请在下面添加评论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM