Django: how to filter by most selling item

Ok, my first thought on this then is to use a Subquery. I’d write a Subquery to find the Max of the number of sales of each course for a creator and return that value to the outer query. The outer query would then filter the queryset for the courses where the number of sales equals that max value.

You may want those values grouped by creator. See the discussion here for some ideas - sum a grouped data (I’m not sure about this - I’m not sure I know right off what you’re going to get as a result of the subquery-style query.)

One other thought I had that may be helpful -

You want to look at this as “Get the list of courses that are the top selling for each creator” and not as “Get each creator and their top-selling course”. This change in perspective is important because you may end up getting multiple courses for a creator.