我有一个带有 created
的 Django 模型时间戳,我想获得每天创建的对象的数量。我希望在 Django 中使用聚合功能,但我不知道如何解决我的问题。假设这不起作用,我总是可以退回到仅使用 values_list 获取所有日期,但我更愿意将工作交给 Django 或 DB。你会怎么做?
最佳答案
亚历克斯在评论中指出了正确的答案:
Count number of records by date in Django
信用转至 ara818Guidoism.objects.extra({'created':"date(created)"}).values('created').annotate(created_count=Count('id'))
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'))
我每天都在学习新的技巧阅读堆栈..太棒了!
关于django - 从 Django 获取每日对象计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4878614/