I am trying to find an effective way to find the rank of an object in the database associated with this account. My naive solution looks like this:
rank = 0 for q in Model.objects.all().order_by('score'): if q.name == 'searching_for_this' return rank rank += 1
It should be possible for the database to filter using order_by:
Model.objects.all (). Order_by ('rating'). Filter (name = 'searching_for_this')
But there seems to be no way to get the index for the order_by step after the filter.
Is there a better way to do this? (Using python / django and / or raw SQL.)
My next thought is to pre-calculate the ranks in the inset, but that seems messy.
python sql django order rank
Bob bob
source share