Django中的时间戳记TruncHour聚合 [英] Timestamp TruncHour aggregation in Django
本文介绍了Django中的时间戳记TruncHour聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有peoplecount和timestamp的数据,我想以小时为单位显示聚合.peoplecount对象的模型如下:
I have a data with peoplecount and timestamp which I want to show aggregated in an hour wise format.The model for peoplecount object is like below:
class PeopleCount(models.Model):
"""
A webapp model classs to store People Count Details.
"""
timestamp = models.DateTimeField(auto_now=True)
people_count_entry = models.IntegerField(blank=True, null=True)
people_count_exit = models.IntegerField(blank=True, null=True)
store = models.ForeignKey(Store, blank=True, null=True)
profile = models.ForeignKey(Profile)
camera = models.ForeignKey(Camera)
recorded_time = models.DateTimeField(null=True, blank=True)
def str(self):
return "People Count {}".format(self.timestamp)
class Meta:
verbose_name = "People Count"
verbose_name_plural = "People Count"
ordering = ['-timestamp']
我正在使用以下查询按小时获取数据:
and I am using below query to get data on hour basis:
queryset = PeopleCount.objects.filter(
**json.loads(
self.request.query_params['filter'])['object_params']
).annotate(
time_series=TruncHour('recorded_time')).values(
'time_series').annotate(
people_count_entry=Sum('people_count_entry')).values(
'time_series',
'people_count_entry').annotate(
people_count_exit=Sum('people_count_exit')).values(
'time_series', 'people_count_entry',
'people_count_exit')
上述查询的问题在于它实际上不是按小时汇总,而是为每个时间戳保留单独的值我必须在客户端进行操作.客户端上的方法可行,但是对于较大的查询集,它需要花费大量时间.希望我的问题陈述清楚.谢谢.
The problem with above query is that it actually doesn't aggregate on hour basis and instead keep individual values for each timestamp which I have to manipulate at client side. The approach on client side works but it takes a lot of time for larger queryset. Hope my problem statement is clear. Thanks.
推荐答案
查看全文