1👍
✅
Try doing this instead:
most_injured = Player.objects.annotate(injury_count=Count('playerinjury')).order_by('-injury_count')[:5]
1👍
You are querying individual PlayerInjury
objects, so of course you have multiple players in your results.
If you Count
by id
, you will always get a count of one since there is only 1 object per id.
If you want player objects ordered by injuries, you need
Player.objects.annotate(count=Count('playerinjury')).order_by('-count')
Source:stackexchange.com