Skip to main content

Debug tool to print SQL queries count to the console

Project description

Django Queries Count

check PyPI version PyPI - Python Version PyPI - Versions from Framework Classifiers

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

duplicates-main-example

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):

pip install Pygments
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

good_example yellow_example

Contribute

Feel free to open an issue to report any bugs. Bug fixes and features are welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_query_counter-0.6.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_query_counter-0.6.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

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

Hashes for django_query_counter-0.6.0.tar.gz
Algorithm Hash digest
SHA256 12e41b5778f5f5843d79056eda197afcdf4b41182d3240317c214d992e3002a2
MD5 686a560b4ce6ff900dc014e2546ce4d0
BLAKE2b-256 06aadb81aa40dd3c4db86bf67889c5de46a29b0966f5d74e9c168177d26200a5

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_query_counter-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_query_counter-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f10111c32f89941b80d60f928cc85b6b8f0a63fd3da0a60c02ecb97df26361c
MD5 1b15dc7f032081eb795bd5c44ac5f9a1
BLAKE2b-256 3c4b6597a65e13ff88f8a06cc7e1d4dc7ad6de66ae351a8ed85b8af6d84ff28b

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page