Debug tool to print SQL queries count to the console
Project description
Django Queries Count
A small debug tool that counts the SQL queries Django runs and prints a summary to the console — including duplicate queries (the usual sign of an N+1 problem) and the slowest ones, similar to what django-debug-toolbar shows in the browser.
Unlike most similar tools, which only ship a middleware, this one also provides a decorator. That means you can profile not just views but also management commands, Celery tasks, or any plain function — which is exactly the use case it was built for.
Example output
The basic idea is to count duplicate queries, like django-debug-toolbar does, and output them. The number of duplicated queries and the color of the theme can be specified in the settings. It is also possible to output all requests at once (counted if they are duplicated).
Content
Installation
It is enough to install the package and apply the decorator to the desired management command or view.
pip install django-query-counter
Please take note that the colored and reformatted SQL output depicted in the readme screenshots may not be achieved unless the specified additional packages are installed (which maybe already installed):
- colorize requires pygments
pip install Pygments
- reformat requires sqlparse
pip install sqlparse
Usage
The project can be used in two ways:
Import the decorator and apply it where you need to know the number of queries to the database.
- management command:
from django.core.management.base import BaseCommand
from query_counter.decorators import queries_counter
class Command(BaseCommand):
@queries_counter
def handle(self, *args, **options):
pass
- function-based views
from query_counter.decorators import queries_counter
@queries_counter
def index(request):
pass
- class-based views:
from django.utils.decorators import method_decorator
from query_counter.decorators import queries_counter
@method_decorator(queries_counter, name='dispatch')
class IndexView(View):
pass
- specifying middleware in settings for all views at once.
MIDDLEWARE = [
'query_counter.middleware.DjangoQueryCounterMiddleware',
]
Available settings
It is possible to override the default settings. To do this, you need to include the app to the INSTALLED_APPS:
INSTALLED_APPS = [
...,
'query_counter',
...
]
Default settings:
{
'DQC_ENABLED': True, # set to False to disable all output (e.g. in production)
'DQC_SLOWEST_COUNT': 5,
'DQC_DUPLICATED_COUNT': 10, # max number of duplicate queries to print
'DQC_TABULATE_FMT': 'pretty',
'DQC_SLOW_THRESHOLD': 1, # seconds
'DQC_INDENT_SQL': True,
'DQC_PYGMENTS_STYLE': 'tango',
'DQC_PRINT_ALL_QUERIES': False,
'DQC_COUNT_QTY_MAP': {
5: 'green',
10: 'white',
20: 'yellow',
30: 'red',
},
}
Feel free to override any of them.
Tabulate tables formats you can find here. Pygments styles available here.
Additional screenshots
Contribute
Feel free to open an issue to report any bugs. Bug fixes and features are welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_query_counter-0.6.0.tar.gz.
File metadata
- Download URL: django_query_counter-0.6.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12e41b5778f5f5843d79056eda197afcdf4b41182d3240317c214d992e3002a2
|
|
| MD5 |
686a560b4ce6ff900dc014e2546ce4d0
|
|
| BLAKE2b-256 |
06aadb81aa40dd3c4db86bf67889c5de46a29b0966f5d74e9c168177d26200a5
|
Provenance
The following attestation bundles were made for django_query_counter-0.6.0.tar.gz:
Publisher:
publish.yml on conformist-mw/django-query-counter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_query_counter-0.6.0.tar.gz -
Subject digest:
12e41b5778f5f5843d79056eda197afcdf4b41182d3240317c214d992e3002a2 - Sigstore transparency entry: 1660086921
- Sigstore integration time:
-
Permalink:
conformist-mw/django-query-counter@ba25862831b0ba219f3c6078b563067d0b289b8b -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/conformist-mw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ba25862831b0ba219f3c6078b563067d0b289b8b -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_query_counter-0.6.0-py3-none-any.whl.
File metadata
- Download URL: django_query_counter-0.6.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f10111c32f89941b80d60f928cc85b6b8f0a63fd3da0a60c02ecb97df26361c
|
|
| MD5 |
1b15dc7f032081eb795bd5c44ac5f9a1
|
|
| BLAKE2b-256 |
3c4b6597a65e13ff88f8a06cc7e1d4dc7ad6de66ae351a8ed85b8af6d84ff28b
|
Provenance
The following attestation bundles were made for django_query_counter-0.6.0-py3-none-any.whl:
Publisher:
publish.yml on conformist-mw/django-query-counter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_query_counter-0.6.0-py3-none-any.whl -
Subject digest:
5f10111c32f89941b80d60f928cc85b6b8f0a63fd3da0a60c02ecb97df26361c - Sigstore transparency entry: 1660086987
- Sigstore integration time:
-
Permalink:
conformist-mw/django-query-counter@ba25862831b0ba219f3c6078b563067d0b289b8b -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/conformist-mw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ba25862831b0ba219f3c6078b563067d0b289b8b -
Trigger Event:
push
-
Statement type: