1

I'm trying to retrieve a QuerySet:

models = MyModel.objects.filter(starttime__lt=sometime) 

When running using VS2013, this for some reason executes the query (taking forever, I later chain more filters..). I know the query executes because when it finishes, the results are available in the Locals window.

When I run this outside VS2013, it runs correctly.

How do I configure (the debugger?) to not do this?

Ronnyle
  • 147
  • 9

2 Answers2

1

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.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • I tried following your suggestion, adding a simple `return str(type(obj))` at the beginning of the `_repr` method (just to see if it works). I can see the expected effect in the Locals window, but it didn't fix the symptom - is there some other place where the iterator is executed? – Ronnyle Mar 12 '14 at 07:21
  • `enum_child_locally` in visualstudio_py_debugger.py - however, this should only be hit when the object is expanded in the Locals window. – Pavel Minaev Mar 12 '14 at 20:44
  • Thanks for your help. Still could not resolve this. I would like to emphasize that when no I don't breakpoint in that context, everything works as expected. I would appreciate any further ideas.. – Ronnyle Mar 13 '14 at 08:00
  • The difference between breakpoint and no breakpoint is as expected - when there's no breakpoint, you don't stop in this context, and therefore debugger doesn't have any reason to inspect the locals in any way. Since this is getting into a rather long-winded discussion that is more about product support, rather than answering your original question, I would suggest moving it over to PTVS forum: https://pytools.codeplex.com/discussions - feel free to start the thread there, and I'll jump in. In the meantime, I'll try to reproduce this locally and see what's triggering the evaluation. – Pavel Minaev Mar 13 '14 at 22:26
0

I'm making a sort of wild guess here (since I do not use windows), here is what I think is going on:

The debugger is printing the contents of your variables for you, the problem is that Django's queryset are evaluated when they are printed, here is the code I think is being executed. I guess you could use breakpoints instead of stepping (assuming that you are stepping) to avoid the printing of the queryset.

Augusto Hack
  • 2,032
  • 18
  • 35