[Answered ]-Remove extra GROUP BY in SQL query in Django ORM

0👍

My solution eventually was to switch to SqlAlchemy for complex queries. This package worked for me well – Aldjemy

👤AmirM

2👍

Have you tried 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)

Its a bit risky solution, but try it.

👤Dan

Leave a comment