Django:计算特定月份的对象数

发布于 2024-09-17 21:06:59 字数 517 浏览 12 评论 0原文

我有这个模型:

class Person(models.Model):
   city = models.CharField(max_length=20, blank=True)
   added_date = models.DateField(default=datetime.date.today)

我想创建一个模板/视图,其中包含月份表和当月添加的人数(即 1 月 5 人、2 月 10 人、3 月 8 人等)。我有一个类似的表,供每个城市的所有人使用:

cities = Patient.objects.values('city').annotate(city_count=Count('city')).order_by('-city_count')

我不知道如何为我的月份表执行此操作。我可以过滤特定月份,然后计算所有月份。但随后我需要每个月循环运行一次,这将是多个数据库命中。我不想那样做。

有没有办法不用写sql而只使用django的api来做到这一点?

I have this model:

class Person(models.Model):
   city = models.CharField(max_length=20, blank=True)
   added_date = models.DateField(default=datetime.date.today)

I want to create a template/view that has a table of months and the number of people added that month (ie, 5 in january, 10 in february, 8 in march, etc.). I have a similar table for all the people from a each city using:

cities = Patient.objects.values('city').annotate(city_count=Count('city')).order_by('-city_count')

I don't know how to do that for my months table. I could filter for a particular month, then count all. But then I'd need to run that through a loop over every month, which would be multiple database hits. I don't want to do that.

Is there a way to do this without writing sql and just using django's api?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-09-24 21:06:59

这是一个非常古老的线程,但我想我会回答,以防其他人最终在这里寻找解决方案。
该解决方案适用于 Django 1.10+ 使用 ExtractMonth 函数,有关更多详细信息,请访问官方 文档

首先,您必须导入 ExtractMonth,例如

from django.db.models.functions import ExtractMonth

然后使用您的 Persons 模型,代码将像这样

personsMonthlyData = Person.objects.annotate(month=ExtractMonth('added_date')).values('month').annotate(count=Count('id')).order_by('month')

PersonMonthlyData 将输出类似这样的内容

[{month: 1, count: 3}, {month: 2: count: 1}]

,其中月份代表月份编号,例如 1 代表一月,2 代表二月,每月的计数分配给计数项目。

我希望这有帮助。

Its a very old thread, but i guess I'll answer in case someone else ended up here looking for a solution.
The solution is for Django 1.10+ using the ExtractMonth function, for more detail visit official documentation

First you have to import ExtractMonth, like

from django.db.models.functions import ExtractMonth

Then using your Persons model, the code will be like this

personsMonthlyData = Person.objects.annotate(month=ExtractMonth('added_date')).values('month').annotate(count=Count('id')).order_by('month')

personsMonthlyData will output something like this

[{month: 1, count: 3}, {month: 2: count: 1}]

where month represent the month number e.g. 1 for January and 2 for February and the count against each month is assigned to the count item.

I hope this helps.

再见回来 2024-09-24 21:06:59

事实上,大多数数据库都有这样一种完美的方法来使用 GROUP BY 查询来执行此操作,而 Django AFAIK 中没有类似的查询,这会导致我使用 SQL 来执行此操作。我在谷歌上搜索了“django sql”,并找到了 Doug Hellman 关于这个确切问题的帖子:http://blog.doughellmann.com/2007/12/using-raw-sql-in-django.html。我会用它作为将月份计数输入 Django 的起点。

The fact that most DBs have such a perfect way of doing this with a GROUP BY query that has no analog in Django AFAIK would lead me to drop into SQL to do this. I did a google search for "django sql" and turned up this post by Doug Hellman on this exact problem: http://blog.doughellmann.com/2007/12/using-raw-sql-in-django.html. I would use that as a starting point for getting your month counts into Django.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文