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