It is not entirely clear whether you're just running this code under debugger, or you're stepping through it (or hitting breakpoints, or something along those lines). If it is the latter, then this is expected behavior - the debugger tries to display the values of all locals in the Locals window, and the way it does that is by calling repr
, and, for collections, peeking at the contents to see whether there are any child items. If the collection is lazily loaded, this would cause it to load.
For PTVS 2.1, we changed this logic somewhat so that objects that can only be iterated once (like generators) are not iterated by the debugger until you try to expand it (see this and this). Unfortunately, I don't think this would apply to QuerySet, as it can be iterated multiple times.
It seems that there are scenarios where it would be useful to explicitly request certain types to not be iterated, and perhaps even repr'd at all. This would be an interesting feature request - can you please file it on our tracker?
In the meantime, you can hack the debugger code locally to do what you want. Have a look at visualstudio_py_util.py
in "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio" (adjusted for your bitness & VS version), class SafeRepr
. You can insert a new branch near the top of the _repr
method that would check for QuerySet
, and print out a custom repr that doesn't trigger lazy loading.