On Sat, Aug 23, 2008 at 3:57 AM, Viktor Nagy <[EMAIL PROTECTED]> wrote:
> is this true or both my test and real code are simply wrong? I was
> kinda shocked from this

The problem here is basically conceptual. You've assumed that, because
it supports some of the same operations as a list, a QuerySet must
*be* a list -- in other words, that it's a container which holds a
collection of objects.

So while the things you're trying would work with a list (and would
work if you used Python's built-in 'list()' function to turn your
QuerySet's results into a list) because a list is simply a static
collection of objects, they *won't* work on a QuerySet because a
QuerySet is actually a representation of some way you're interacting
with your database, and as a result has to walk some fine lines
between not hitting your DB too often and not giving you stale
results.

As a general rule, then, whenever you want this sort of behavior, either:

1. Use list() to force your QuerySet into a list of objects you can
then work with as a list, or
2. Pull out the object or objects you want, and store them in a
variable, because there are situations (like this one) where the
QuerySet won't automatically "remember" them for you.

The slightly longer technical answer, if you're interested, is that
what you're trying will, sort of by accident, work *if and only if*
you've already done something which forced the QuerySet to evaluate,
perform its query and populate its internal result list. If that
hasn't happened yet, repeatedly slicing or indexing into the QuerySet
will result in a new query being run each time you do this, with no
"remembering" of previously-returned results.


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to