如何从Django auth中收集每天登记数量的条形图数据?

时间:2022-12-04 15:51:21

Django auth User model has a date_joined DateTimeField. Could this be used to aggregate a list with volumes of registrations per day for days (or other time periods) in a daterange? E.g.: [(21.01, 5), (22.01, 7), (23.01, 9), ...] What is the fastest way to do this? E.g. if the date range on the barchart was set to past 3 years.

Django auth用户模型有一个date_join DateTimeField。这是否可以用于将每天的注册量(或其他时间段)聚合到一个日期范围内?例:[(21.01,5),(22.01,7),(23.01,9),…做这件事最快的方法是什么?例如,如果在barchart上的日期范围设置为过去3年。

I looked at http://docs.djangoproject.com/en/dev/topics/db/aggregation/, but it's not apparent how to breakdown objects using their timestamps. This is a common design pattern in statistics.

我查看了http://docs.djangoproject.com/en/dev/topics/db/aggregation/,但是不清楚如何使用时间戳分解对象。这是统计中常见的设计模式。

2 个解决方案

#1


1  

This question is similar to this one: django: time range based aggregate query

这个问题与这个问题类似:django:基于时间范围的聚合查询

Something like this should work: MyObject.objects.filter(date_joined__date__range=(weekago,today)).annotate(registrations=Count('id')

类似这样的东西应该可以工作:MyObject.objects.filter(date_joined__date__range=(weekago,today)).annotate(registrations=Count('id'))

#2


0  

Here's my code for generating date-based barcharts with Django and CSS.

下面是我用Django和CSS生成基于数据的条形图的代码。

def date_barchart(model, date_field, operation='count', days=150, till=datetime.now()):   
    list = []
    i = 0
    while i < days:
        day = (till - timedelta(days=i), till - timedelta(days=i-1))
        date = time.strftime("%Y-%m-%d", day[0].timetuple())
        kwargs = {'%s__range' % date_field: day}
        qs = model.objects.filter(**kwargs)
        if operation == 'count':
            total = qs.count()
        else:
            total = qs.aggregate(Sum('amount'))
            total = total['amount__sum']
        if total > 0:
            list.insert(0, {'date': date, 'total': total, 'round_total': int(total)})

        i = i + 1
    return list

In my view I add may barchart data to template's context:

在我看来,我将may barchart数据添加到template的上下文中:

'barchart': date_barchart(User, 'date_joined')

or for Satchmo e-commerce

电子商务或书包嘴

'barchart': date_barchart(Payment, 'time_stamp', 'sum', 150)

template:

模板:

{% if barchart %}
<div class="barchart">
    <ol>
        {% for d in barchart %}
        <li><u style="height:{{ d.round_total }}px"><i>{{ d.date }}<br>
            <b>{{ d.total }}</b></i></u></li>
        {% endfor %}
    </ol>
</div>
{% endif %}

and CSS:

和CSS:

.barchart{width:100%;overflow:hidden}
.barchart u{display:inline-block;position:relative;vertical-align:bottom;width:5px;background:#6cc;border-right:1px solid #fff;text-decoration:none}
.barchart u i{display:none;position:absolute;background:#fff;border:1px solid #aaa;padding:5px;border-radius:5px;left:-6px;bottom:10px;width:60px;font-size:10px;font-style:normal;color:#000}
.barchart u:hover{background-color:#333}
.barchart u:hover i{display:inline-block;z-index:500}
.barchart li{margin:0;padding:0;list-style:none;display:inline-block}

Looks as good as Google analytics, but no need to give away my performance data.

看起来和谷歌分析一样好,但不需要泄露我的性能数据。

#1


1  

This question is similar to this one: django: time range based aggregate query

这个问题与这个问题类似:django:基于时间范围的聚合查询

Something like this should work: MyObject.objects.filter(date_joined__date__range=(weekago,today)).annotate(registrations=Count('id')

类似这样的东西应该可以工作:MyObject.objects.filter(date_joined__date__range=(weekago,today)).annotate(registrations=Count('id'))

#2


0  

Here's my code for generating date-based barcharts with Django and CSS.

下面是我用Django和CSS生成基于数据的条形图的代码。

def date_barchart(model, date_field, operation='count', days=150, till=datetime.now()):   
    list = []
    i = 0
    while i < days:
        day = (till - timedelta(days=i), till - timedelta(days=i-1))
        date = time.strftime("%Y-%m-%d", day[0].timetuple())
        kwargs = {'%s__range' % date_field: day}
        qs = model.objects.filter(**kwargs)
        if operation == 'count':
            total = qs.count()
        else:
            total = qs.aggregate(Sum('amount'))
            total = total['amount__sum']
        if total > 0:
            list.insert(0, {'date': date, 'total': total, 'round_total': int(total)})

        i = i + 1
    return list

In my view I add may barchart data to template's context:

在我看来,我将may barchart数据添加到template的上下文中:

'barchart': date_barchart(User, 'date_joined')

or for Satchmo e-commerce

电子商务或书包嘴

'barchart': date_barchart(Payment, 'time_stamp', 'sum', 150)

template:

模板:

{% if barchart %}
<div class="barchart">
    <ol>
        {% for d in barchart %}
        <li><u style="height:{{ d.round_total }}px"><i>{{ d.date }}<br>
            <b>{{ d.total }}</b></i></u></li>
        {% endfor %}
    </ol>
</div>
{% endif %}

and CSS:

和CSS:

.barchart{width:100%;overflow:hidden}
.barchart u{display:inline-block;position:relative;vertical-align:bottom;width:5px;background:#6cc;border-right:1px solid #fff;text-decoration:none}
.barchart u i{display:none;position:absolute;background:#fff;border:1px solid #aaa;padding:5px;border-radius:5px;left:-6px;bottom:10px;width:60px;font-size:10px;font-style:normal;color:#000}
.barchart u:hover{background-color:#333}
.barchart u:hover i{display:inline-block;z-index:500}
.barchart li{margin:0;padding:0;list-style:none;display:inline-block}

Looks as good as Google analytics, but no need to give away my performance data.

看起来和谷歌分析一样好,但不需要泄露我的性能数据。