> 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.
No comments:
Post a Comment