Get the index of an element in a queryset

I have a QuerySet, let's call it qs, which is ordered by some attribute which is irrelevant to this problem. Then I have an object, let's call it obj. Now I'd like to know at what index obj has in qs, as efficiently as possible. I know that I could use .index() from Python or possibly loop through qs comparing each object to obj, but what is the best way to go about doing this? I'm looking for high performance and that's my only criteria.

Using Python 2.6.2 with Django 1.0.2 on Windows.


Solution 1:

If you're already iterating over the queryset and just want to know the index of the element you're currently on, the compact and probably the most efficient solution is:

for index, item in enumerate(your_queryset):
    ...

However, don't use this if you have a queryset and an object obtained by some unrelated means, and want to learn the position of this object in the queryset (if it's even there).

Solution 2:

If you just want to know where you object sits amongst all others (e.g. when determining rank), you can do it quickly by counting the objects before you:

    index = MyModel.objects.filter(sortField__lt = myObject.sortField).count()

Solution 3:

QuerySets in Django are actually generators, not lists (for further details, see Django documentation on QuerySets).
As such, there is no shortcut to get the index of an element, and I think a plain iteration is the best way to do it.

For starter, I would implement your requirement in the simplest way possible (like iterating); if you really have performance issues, then I would use some different approach, like building a queryset with a smaller amount of fields, or whatever.
In any case, the idea is to leave such tricks as late as possible, when you definitely knows you need them.
Update: You may want to use directly some SQL statement to get the rownumber (something lie . However, Django's ORM does not support this natively and you have to use a raw SQL query (see documentation). I think this could be the best option, but again - only if you really see a real performance issue.