In case you're interested and want to see this in 2.0, please help: https://code.djangoproject.com/ticket/28595 https://github.com/django/django/pull/9078
On Friday 14 April 2017 02:33:06 Adam Johnson wrote: > django-perf-rec <https://github.com/yplan/django-perf-rec> would love this, > it currently monkey patches connection.ops.last_executed_query to listen to > all the queries going on. > > On 7 April 2017 at 16:10, Shai Berger <s...@platonix.com> wrote: > > On Friday 07 April 2017 17:47:51 Carl Meyer wrote: > > > Hi Shai, > > > > > > On 04/07/2017 06:02 AM, Shai Berger wrote: > > > > This is an idea that came up during the djangocon-europe conference: > > Add > > > > > > the ability to install general instrumentation hooks around the > > > > database > > > > > > "execute" and "executemany" calls. > > > > > > > > Such hooks would allow all sorts of interesting features. For one, > > > > they could replace the current special-case allowing > > > > assertNumQueries & friends to record queries out of debug mode (it's > > > > an ugly hack, > > > > really), > > > > > > but they could also support my imagined use case -- a context-manager > > > > which could prevent database access during execution of some code > > > > (I'm thinking mostly of using it around "render()" calls and > > > > serialization, to make sure all database access is being done in the > > > > view). > > > > > > Another use-case is for preventing database access during tests unless > > > specifically requested by the test (e.g. pytest-django does this, > > > currently via monkeypatching). > > > > Yep. This feels right. > > > > > > My idea for implementation is to keep a thread-local stack of > > > > context- managers, and have them wrap each call of "execute". We > > > > could actually even use one such context-manager instead of the > > > > existing > > > > CursorDebugWrapper. > > > > > > Why a new thread-local instead of explicitly per-connection and stored > > > on the connection? > > > > Sorry for implying that it would be a new thread-local, I just hadn't > > thought > > it through when I wrote this. Of course it goes on the (already > > thread-local) > > connection. > > > > Shai.