Re: about QuerySet
so it means when i call user[1] after user[0], it is possible that i
will get same record if someone else insert a new record into database
between 2 calls.
On Feb 1, 5:34 pm, akaariai <akaar...@gmail.com> wrote:
> On Feb 1, 9:36 am, newme <dllizh...@gmail.com> wrote:
>
> > do you mean that queryset will query database every time i call
> > user[0]?
>
> Yes. That is exactly what happens:
> In [7]: qs[0]
> Out[7]: <OrganisaatioOsa: THL - Terveyden ja hyvinvoinnin laitos>
> In [9]: print connection.queries
> [{'time': '0.011', 'sql': 'SELECT ... FROM "organisaatio_osa" LIMIT
> 1'}]
>
> In [10]: qs[0]
> Out[10]: <OrganisaatioOsa: THL - Terveyden ja hyvinvoinnin laitos>
> In [11]: print connection.queries
> [{'time': '0.011', 'sql': 'SELECT ... FROM "organisaatio_osa" LIMIT
> 1'},
> {'time': '0.001', 'sql': 'SELECT ... FROM "organisaatio_osa" LIMIT
> 1'}]
>
> If you do not want this to happen, you can evaluate your queryset into
> a list first by:
> objlist = list(qs[0:wanted_limit])
> and now objlist is just a regular Python list.
>
> The lazy evaluation of querysets can be a little surprising sometimes.
> Using django-debug-toolbar or just settings.DEBUG = True, and then
> print connection.queries is recommended :)
>
> - Anssi
--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home