I need to get the top stock tickers followed by users, the issue is when two users or more follow the same ticker, it get repeated when using the query Tickers.objects.annotate(followers=Count('users')).order_by('followers')
as shown here:
In this example, the ticker FDCFF
needs to be the top ticker since two users are following it, instead it gets repeated and its not being shown as the top followed ticker.
Models.py
class Tickers(models.Model):
symbol = models.CharField(max_length=100)
name = models.CharField(max_length=100)
users = models.ManyToManyField(User, related_name='tickers')
views.py
def index(request):
tickers = Tickers.objects.annotate(followers=Count('users')).order_by('followers')
context = {
'tickers': tickers,
}
return render(request, 'main_index.html', context=context)