GithubHelp home page GithubHelp logo

rajat4665 / django-query-counter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from conformist-mw/django-query-counter

0.0 0.0 0.0 38 KB

Simple tool to count Django db queries in the management commands and views (as decorator or middleware)

Home Page: https://pypi.org/project/django-query-counter/

License: MIT License

Python 100.00%

django-query-counter's Introduction

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.

Release

python setup.py sdist bdist_wheel
twine check dist/*
twine upload dist/*

django-query-counter's People

Contributors

claggierk avatar conformist-mw avatar dmytrolitvinov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.