【发布时间】:2021-06-16 09:27:27
【问题描述】:
假设我们有模型:
class Category(models.Model):
description = models.CharField(...) # Ex: 'horror', 'classic', 'self-help', etc.
class Book(models.Model):
category = models.ForeignKey(Category, ...)
written_date = models.DateField(...)
我想做一个查询,最终得到我每年每个类别的图书总数!像这样:
{
'2019-01-01': { 'horror': 2, 'classic': 1},
'2020-01-01': { 'horror': 2, 'classic': 1, 'self-help': 4},
...
}
我只能提出以下查询:
Book.objects \
.annotate(year=TruncYear('written_date')) \
.values('year', 'category__description') \
.order_by('year') \
.annotate(total=Count('id'))
但这只会让我明白
{
{
"category__description": "Horror",
"year": "2019-01-01",
"total": 2
},
{
"category__description": "Classic",
"year": "2019-01-01",
"total": 1
},
{
"category__description": "Horror",
"year": "2020-01-01",
"total": 2
},
...
}
有没有办法通过 ORM 做到这一点?或者我必须通过直接操作结果来做到这一点?谢谢!
【问题讨论】:
-
不要真的相信有任何 ORM 解决方案(事实上,即使您查看 SQL,也只会得到表格结果)。您可以在视图中执行分组(Willem 的解决方案),在模板中使用
regrouptemplate tag 进行分组,或者使用ifchangedtemplate tag 给出分组的错觉。我建议使用威廉的解决方案。