Skip to main content

Debug tool to print sql queries count to the console

Project description

Django Queries Count

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

Installation

It is enough to install the package and apply the decorator to the desired management command or view.

pip install django-query-counter

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

    @query_counter
    def handle(self, *args, **options):
        pass
  • general view:

    • 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.1.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

django_query_counter-0.1.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file django-query-counter-0.1.2.tar.gz.

File metadata

  • Download URL: django-query-counter-0.1.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django-query-counter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 abbed5a348727bb120096e3733fadf59c1cf165e4a007e2ff9bca21775a8f2bf
MD5 fd89e5a5a8ad117ed3ef02d88806be51
BLAKE2b-256 1fd4b1dec2f3932c969a7cbf08e26e06cbb98439c430846aafc7dd6cd1e5aa86

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_query_counter-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for django_query_counter-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 25c2e38ad4abeba7558ab6ee5fceb42f43a8ba8b20d420ad96a4bae6394d1230
MD5 9441a76fd72d191fb0c4153a62743b6d
BLAKE2b-256 9e03885285f0a247c780a087ff4f5d01e1bb31f4467611406122303a96374cd3

See more details on using hashes here.

Supported by

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