Package: django-query-debugger
django-query-debuggerPrints queries executed on you projects along with line traceback.Table of ContentsWhy Should I Use This?When Should I NOT Use This?How ToFeaturesUsageSingleton moduleOn Django shellTraceback featureWhy Should I Use This?Django ORM is great but ccan be a little obscure sometimes. Sometimes you want to know what queries your project is doing, some other times you see some weird looking querie running on your DB logs and have no ideia what triggered. This little lib helps with that.When Should I NOT Use This?On your production environment. This guy is working fine but you dont need to insert this lame-hacking-failure-point into you production code, do you?How ToInstall withpip install django-query-debugger-Then just import this lib withimportquery_debuggerSee theUsagesection for more informationBut remember:Do not use it on production. I love this little hack but IT'S NOT NEEDED FOR PRODUCTIONFeaturesPrint out EVERY query you make using django ORM or django.db.connection directlyPrint out EVERY query make by your, your framework or external libsPrint out ONLY query trigged by a specific filePrint usable strings for debugTraceback query execution to related filesUsage:Singleton module:As Python's modules are singleton import this lib anywhere and it will affect the who project.For example, when you hit a simple django view like this one:"""myproject/myapp/views.py"""fromdjango.httpimportHttp404fromdjango.shortcutsimportrenderfrommyapp.modelsimportMyModelimportquery_debuggerdefdetail(request,my_model_id):try:p=MyModel.objects.get(pk=my_model_id)exceptMyModel.DoesNotExist:raiseHttp404("MyModel does not exist")returnrender(request,'mymodel/detail.html',{'mymodel':p})Your server output will look like this:[12:03:23]myproject$ ./manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
May 15, 2019 - 11:03:31
Django version 2.1, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[11:03:38] INFO "GET /mymodel/1 HTTP/1.1" 200 38346
  /Users/fabio/projects/myproject/myapp/views.py Line: 7
    SELECT "myapp_mymodel"."id", "myapp_mymodel"."name" FROM "myapp_mymodel" WHERE "myapp_mymodel"."id" = 1On Django shell>>>importquery_debugger>>>frommyapp.modelsimportMyModel>>>MyModel.objects.count()/Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/sql/query.pyLine:483/Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/sql/compiler.pyLine:1061/Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/backends/utils.pyLine:100SELECTCOUNT(*)AS"__count"FROM"myapp_mymodel"3Traceback featureThe default behavior is to traceback only queries trigged by your code, ommiting queries trigged by the framework or libraries. However, if you want you can expand to any querie trigged anywhere like this:Max DepthA max_depth argument may be added to query_debugger.everywhere(max_depth=10) or query_debugger.here(max_depth=10) to change how deep the traceback will go.Ex: max_depth=50[12:03:23]myproject$ ./manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
  /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/query.py Line: 138
    /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/sql/compiler.py Line: 1061
      /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/backends/utils.py Line: 100
        SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"
May 15, 2019 - 11:15:57
Django version 2.1, using settings 'thundera.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[11:03:38] INFO "GET /mymodel/1 HTTP/1.1" 200 38346
  /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/query.py Line: 54
    /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/sql/compiler.py Line: 1061
      /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/backends/utils.py Line: 100
        SELECT "myapp_mymodel"."id", "myapp_mymodel"."name" FROM "myapp_mymodel" WHERE "myapp_mymodel"."id" = 1Ex: max_depth=0[12:03:23]myproject$ ./manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
  SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"
May 15, 2019 - 11:15:57
Django version 2.1, using settings 'thundera.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[11:03:38] INFO "GET /mymodel/1 HTTP/1.1" 200 38346
  SELECT "myapp_mymodel"."id", "myapp_mymodel"."name" FROM "myapp_mymodel" WHERE "myapp_mymodel"."id" = 1everywhere()"""myproject/myapp/views.py"""importquery_debuggerquery_debugger.everywhere()...Your server output will look like this:[12:03:23]myproject$ ./manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
  /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/query.py Line: 138
    /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/sql/compiler.py Line: 1061
      /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/backends/utils.py Line: 100
        SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"
May 15, 2019 - 11:15:57
Django version 2.1, using settings 'thundera.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[11:03:38] INFO "GET /mymodel/1 HTTP/1.1" 200 38346
  /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/query.py Line: 54
    /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/sql/compiler.py Line: 1061
      /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/backends/utils.py Line: 100
        SELECT "myapp_mymodel"."id", "myapp_mymodel"."name" FROM "myapp_mymodel" WHERE "myapp_mymodel"."id" = 1here()Narrow down to the file where you imported the debugger:"""myproject/myapp/views.py"""importquery_debuggerquery_debugger.here()# will only print queries trigged by myproject/myapp/views.py...