在 Django ORM 中删除 SQL 查询中的额外 GROUP BY

Remove extra GROUP BY in SQL query in Django ORM

我有以下型号:

class Statistics(models.Model):
    game_id = models.IntegerField(db_column='gameid', primary_key=True)
    time = models.DateTimeField()
    servers = models.IntegerField()
    users = models.IntegerField()

我有带有自定义数据库功能的遗留 postgre 数据库 datediff_hours。 为了在 django 中使用这个函数,我创建了以下 class:

from django.db.models import Func


class DateDiffHoursFunc(Func):
    function = 'datediff_hours'

现在我构造实际的查询:

from django.db.models import Sum, Value, F

query = Statistics.objects.values('time').filter(game_id=1).\
        annotate(users_sum=Sum('users', distinct=True)).\
        annotate(timestamp=DateDiffHoursFunc(Value(datetime(2016, 5, 26)), F('time'))).\
        values('users_sum', 'timestamp')

我得到的查询:

>>> print(query.query)

SELECT 
     SUM(`statistics`.`users`) AS `users_sum`, 
     datediff_hours(2013-04-01 00:00:00, `statistics`.`time`) AS `timestamp`
FROM `statistics` WHERE `statistics`.`gameid` = 12 
GROUP BY `statistics`.`time`, datediff_hours(2013-04-01 00:00:00, `statistics`.`time`) 
ORDER BY NULL

但是我只需要按 time 分组,就像这样:

SELECT 
     SUM(`statistics`.`users`) AS `users_sum`, 
     datediff_hours(2013-04-01 00:00:00, `statistics`.`time`) AS `timestamp`
FROM `statistics` WHERE `statistics`.`gameid` = 12 
GROUP BY `statistics`.`time`) 
ORDER BY NULL

如何限制仅按 time 分组?

你试过 Raw SQL 吗?

    query = Statistics.objects.values('time').filter(game_id=1).\
            annotate(users_sum=Sum('users', distinct=True)).\
            annotate(timestamp=DateDiffHoursFunc(Value(datetime(2016, 05, 26)), F('time'))).\
            values('users_sum', 'timestamp').query
    query.group_by = ['time']
    objs = QuerySet(query=query, models=Statistics)

这是一个有点冒险的解决方案,但请尝试一下。

我的解决方案最终是针对复杂查询切换到 SqlAlchemy。这个包对我很有效 - Aldjemy