从Django获取每日对象数量 [英] Get daily counts of objects from Django

查看:307
本文介绍了从Django获取每日对象数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有创建的Django模型 timestamp,我想得到每一天创建的对象数。我希望在Django中使用聚合功能,但是我无法弄清楚如何解决我的问题。假设不起作用,我总是可以回到刚刚得到values_list的所有日期,但我更愿意把工作交给Django或DB。你会怎么做?

I have a Django model with a created timestamp and I'd like to get the counts of objects created on each day. I was hoping to use the aggregation functionality in Django but I can't figure out how to solve my problem with it. Assuming that doesn't work I can always fall back to just getting all of the dates with values_list but I'd prefer to give the work to Django or the DB. How would you do it?

推荐答案

Alex在评论中指出正确的答案:

Alex pointed to the right answer in the comment:

计算Django中按日期记录的数量

信用转至 ara818

Guidoism.objects.extra。({ '创造': 日期(创建)})( '创造')值注释(created_count =计数( '编号'))

from django.db.models import Count

Guidoism.objects \
    # get specific dates (not hours for example) and store in "created" 
    .extra({'created':"date(created)"})
    # get a values list of only "created" defined earlier
    .values('created')
    # annotate each day by Count of Guidoism objects
    .annotate(created_count=Count('id'))

我学习新的三重奏ks每天阅读栈..真棒!

I learn new tricks every day reading stack.. awesome!

这篇关于从Django获取每日对象数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆