【发布时间】:2016-04-25 20:21:28
【问题描述】:
有没有办法使用 Django ORM 根据按年/月分组的 date_joined 获取 User count()?
我能够在我的 Django/Postgres 项目中使用原始 SQL 获取这些数据,如下所示:
from django.db import connection
...
cursor = connection.cursor()
cursor.execute('''
SELECT
to_char(date_joined, 'YYYY/MM') as month,
cast(count(id) as int) as total
FROM users_user
GROUP BY month
ORDER BY month DESC
''')
返回给我的列表如下:
[('2015/12', 105), ('2016/01' , 78), ('2016/02', 95)...]
【问题讨论】:
标签: django postgresql django-orm