Django query ordered objects given position
<p>I have a function which takes an object <code>id</code>. With this id I can get the object of a table <code>object_1 = Object.objects.get(pk=id)</code>. I'd like to have the <code>Object</code> table ordered by a position called votes. For example, <code>objects = Object.objects.order_by('-votes')</code> and I'd like to find the position of <code>object_1</code> in <code>objects</code>.</p>

<p>How could I achieve that? As far as I know the Query Set is not a list, and I'd have to iterate over all elements, which would be a big performance issue when I have a lot of entries in the table.</p>

<p>The purpouse of all this is to show a ranking ordered by votes, in which I only show 6 entries and the 4th one should be the one of the object. For example if I searched for the object 1, I should show this:</p>

<pre><code>Position | Name | Votes
   5      obj5     52
   6      obj3     49
   7      obj10    49
   8      obj1     47
   9      obj15    15
   10     obj6     14
</code></pre>

<p>So I'd like to get a query set with <code>obj5,obj3,obj10,obj1,obj15,obj6</code> in this order.
The optimal solution would be doing something like: <code>objects = Object.objects.order_by('-votes')[x-4:x+2]</code> where <code>x</code> is the position of the object I'm searching.</p>

<p>p.s: I'm using Python 2.7.9 and Django 1.9</p>