Using group_by to find sum in Django
I'm working on a website to track music practice and I'm trying to set up a query to show how many minutes you've spent with a certain instrument or song. I modeled this behavior using Khan Academy (to test the SQL):
https://www.khanacademy.org/computer-programming/music-tracker/5599297716994048
but can't find a way to do it in Django. The SQL query would look something like this:
SELECT song, SUM(duration) AS total_time_practiced FROM logs GROUP BY song ORDER BY total_time_practiced DESC
How does this translate to Django? I've tried a few things and the most promising is:
Log.objects.distinct().annotate(total_practice_time=Sum("duration"))
but it still doesn't work. I've checked the docs and Django doesn't seem to have a GROUP BY method for QuerySets. Any advice? This is from one database, by the way, not using any joins.
(apologies for the lack of syntax highlighting on the code snippets; every time I tried to add rich formatting, the webpage crashed)