Skip to main content

django-query-debugger

Prints queries executed on you projects along with line traceback.

Table of Contents

  1. Why Should I Use This?
  2. When Should I NOT Use This?
  3. How To
  4. Features
  5. Usage
    1. Singleton module
    2. On Django shell
    3. Traceback feature

Why 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 To

  • Install with
pip install django-query-debugger

-Then just import this lib with

import query_debugger

See the Usage section for more information

But remember:

Do not use it on production. I love this little hack but IT'S NOT NEEDED FOR PRODUCTION

Features

  • Print out EVERY query you make using django ORM or django.db.connection directly
  • Print out EVERY query make by your, your framework or external libs
  • Print out ONLY query trigged by a specific file
  • Print usable strings for debug
  • Traceback query execution to related files

Usage:

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"""
from django.http import Http404
from django.shortcuts import render
from myapp.models import MyModel
import query_debugger

def detail(request, my_model_id):
    try:
        p = MyModel.objects.get(pk=my_model_id)
    except MyModel.DoesNotExist:
        raise Http404("MyModel does not exist")
    return render(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" = 1

On Django shell

>>> import query_debugger
>>> from myapp.models import MyModel
>>> MyModel.objects.count()
  /Users/fabio/envs/py36/lib/python3.6/site-packages/django/db/models/sql/query.py Line: 483
    /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 COUNT(*) AS "__count" FROM "myapp_mymodel"
3

Traceback feature

  • The 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 Depth

A 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" = 1

Ex: 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" = 1

everywhere()

"""myproject/myapp/views.py"""
import query_debugger
query_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" = 1

here()

Narrow down to the file where you imported the debugger:

"""myproject/myapp/views.py"""
import query_debugger
query_debugger.here()  # will only print queries trigged by myproject/myapp/views.py

...

Release files for django-query-debugger 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-query-debugger 0.0.4
File Size Uploaded
django-query-debugger-0.0.4.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-query-debugger 0.0.4
File Interpreter ABI Platform
django_query_debugger-0.0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 9.5 kB

Release files / django-query-debugger-0.0.4.tar.gz

Download URL django-query-debugger-0.0.4.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
689975d39b08f20f7144754c33175207d931b4b47ae1e9e9df38701a48dae691
BLAKE2b-256 checksum
How to use checksums
98ed698e4911191aec002dd43b78b4d92b0cd02f49bc7e4a08c14fd682357c3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

Release files / django_query_debugger-0.0.4-py2.py3-none-any.whl

Download URL django_query_debugger-0.0.4-py2.py3-none-any.whl
Size 4.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
774ec9e10a11982a67f87765213231f78375430d0accb1df7c2e2df570e33d26
BLAKE2b-256 checksum
How to use checksums
a2c265eea200b9f952f05e1e0b8854537a33cc04e69d06d215cd31b21505c5fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page