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

The difference between this project and all the others like it is that I needed to debug management command in Django, but all the others only provided middleware, which did not solve my problem.

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_SLOWEST_COUNT': 5,
    '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 of any bugs. Bug fixes and features are welcome! Be sure to add yourself to the AUTHORS.md if you provide PR.

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.4.2.tar.gz (6.4 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.4.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file django_query_counter-0.4.2.tar.gz.

File metadata

  • Download URL: django_query_counter-0.4.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_query_counter-0.4.2.tar.gz
Algorithm Hash digest
SHA256 4405bcaef0df00059011f1c9c163584f25d5c36f1bf0b15e09c3a140454f05ce
MD5 3c6e2e647889740415f36e7287d748b2
BLAKE2b-256 8aead5c08ae83522dca83d4d80b4adb491a5f05c2b9f916d7e45d43ebdb497aa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_query_counter-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_query_counter-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6cc843e274b2ad8ad5e90e293e6408cf036af31ee94348d273209538a1c6794c
MD5 53cf3c9aee86781f5756c55562404b68
BLAKE2b-256 441685fb6b040aeddd8b8493436d2711e795fb6de6fcec8b2fa1faf4e1ad7deb

See more details on using hashes here.

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