Re: QuerySets are scary!

2008-08-25 Thread V

thanks for the exhausting answer!

I thought that something similar is going on under the hood, and just
wanted to point it out to other ignorant people like myself.

thanks once again!
V

On Aug 23, 12:27 pm, "James Bennett" <[EMAIL PROTECTED]> wrote:
> 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
-~--~~~~--~~--~--~---



Re: QuerySets are scary!

2008-08-23 Thread James Bennett

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
-~--~~~~--~~--~--~---



Re: QuerySets are scary!

2008-08-23 Thread Vitja Makarov

2008/8/23 Viktor Nagy <[EMAIL PROTECTED]>:
> I just noticed that
>
>yourqueryset[0].save()
>
> is not the same as
>
>yourqueryset0 = yourqueryset[0]
>yourqueryset0.save()
>
> at least the first didn't gave the expected outcome for me
>
> see the attached test (it is enough to look at app/tests.py)
>
> is this true or both my test and real code are simply wrong? I was
> kinda shocked from this
>
> V
>

It seems that yourqueryset[0] gives always the original one object,
stored in DB. e.g. each time SELECT is called and no fields are
changed at save() point.

vitja.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



QuerySets are scary!

2008-08-23 Thread Viktor Nagy
I just noticed that

yourqueryset[0].save()

is not the same as

yourqueryset0 = yourqueryset[0]
yourqueryset0.save()

at least the first didn't gave the expected outcome for me

see the attached test (it is enough to look at app/tests.py)

is this true or both my test and real code are simply wrong? I was
kinda shocked from this

V

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



testbool.tar.gz
Description: GNU Zip compressed data