I'm using django-relationships and I'm filtering my users by number of followers like this:
Relationship.objects.values('to_user').annotate(num_followers=Count('to_user')).order_by('-num_followers')
which returns something like this
[{'to_user': 1, 'num_followers': 3}, {'to_user': 4, 'num_followers': 1}]
My problem is that I need access to my User object, not just their pk. Right now I'm doing something like this:
tu_list = []
for tu in top_users_set:
tu_list.append({
'top_user': User.objects.get(pk=tu['to_user']),
'followers': tu['num_followers'],
})
which is doing a query for every user. Since the queryset could end up having hundreds+ users, this could be really bad.
Any input to help improve this would be appreciated.
Thanks