views:

429

answers:

3

Hi

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.

+3  A: 

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.

Roberto Liffredo
I see. It has been a while since I last touched SQL, but I figured maybe it would be possible in plain SQL and therefore possible using Django's QuerySet API.
Deniz Dogan
Yes, that could be an option. I have added it to the possible solutions.
Roberto Liffredo
+1  A: 

Compact and probably the most efficient:

for index, item in enumerate(your_queryset):
    ...
drdaeman
Isn't "for index, item in enumerate(your_queryset):" better than either of your two alternatives?
Vinay Sajip
Yes, certainly. I completely forgot about it.
drdaeman
Feel free to edit your answer to feature the better option in place of the worse ones - Stack Overflow is all about continually improving answers. If you don't I might do it for you :-)
Carl Meyer
A: 

Assuming for the purpose of illustration that your models are standard with a primary key id, then evaluating

list(qs.values_list('id', flat=True)).index(obj.id)

will find the index of obj in qs. While the use of list evaluates the queryset, it evaluates not the original queryset but a derived queryset. This evaluation runs a SQL query to get the id fields only, not wasting time fetching other fields.

Vinay Sajip
This may be more efficient or it may be less efficient than just evaluating the original queryset, depending on whether you were going to do that anyway later on (since querysets cache their results).
Carl Meyer