简体   繁体   English

Django查询每天计数唯一的新结果

[英]Django query count unique NEW results per day

I'm keeping statistics of my app in a database in a model that looks like this 我将应用程序的统计信息保存在数据库中,模型如下所示

class MyStats():
    event_code = django.db.models.PositiveSmallIntegerField()
    timestamp = django.db.models.DateTimeField()
    timestamp_date = django.db.models.DateField()
    device_id = django.db.models.CharField(max_length=32)

I would like to use this data to determine, for each day, how many NEW app installations I have. 我想使用这些数据来确定每天有多少新应用安装。

I got as far as this: 我得到的是:

MyStats.objects.order_by('-timestamp_date').values('timestamp_date').annotate(count_total=Count('device_id', distinct=True))

But what it seems to give me is the amount of unique users per DAY, which is not desired. 但是似乎给我的是每天的唯一身份用户数量,这是不希望的。 Any hints? 有什么提示吗?

"New" really means "Occurring after x_timedelta ago": “新”实际上意味着“发生在x_timedelta前”:

import datetime

x_timedelta = datetime.timedelta(days=1)

x_timedelta_ago = datetime.datetime.now() - x_timedelta

your_query = MyStats.objects.filter(timestamp_date__gt=x_timedelta_ago)

your_query_distinct = your_query.order_by('-timestamp').annotate(count_total=Count('device_id', distinct=True)).values()

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

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