Here is my solution to the problem. First, here is a query that will capture place identifiers and ratings corresponding to the respective users.
testquery = ActiveVenue.objects.values("venue").annotate(score=Count("event__user", distinct=True)).order_by("-score")
Result
[{'score': 3, 'venue': 2}, {'score': 2, 'venue': 3}, {'score': 1, 'venue': 1}]
Placement IDs will then be placed on the new list.
query_ids = [item["venue"] for item in testquery] [2, 3, 1]
Then you need to get the corresponding objects of the object.
tempresult = Venue.objects.filter(id__in=query_ids) [<Venue: The Hill>, <Venue: The Oaks>, <Venue: The Pound>
Finally, the list will be rethought to reorder Venue objects based on previous estimates.
result = [venue for venue_id in query_ids for venue in tempresult if venue_id == venue.id] [<Venue: The Oaks>, <Venue: The Pound>, <Venue: The Hill>]
This gives the correct result based on test data.
source share