Giter Club home page Giter Club logo

django-querycount's Introduction

Django Querycount

Current Release License

Inspired by this post by David Szotten, this project gives you a middleware that prints DB query counts in Django's runserver console output.

django-querycount in action

Installation

pip install django-querycount

Just add querycount.middleware.QueryCountMiddleware to your MIDDLEWARE_CLASSES.

Notice that django-querycount is hard coded to work only in DEBUG mode set to true

Settings

There are two possible settings for this app: The first defines threshold values used to color output, while the second allows you customize requests that will be ignored by the middleware. The default settings are:

QUERYCOUNT = {
    'THRESHOLDS': {
        'MEDIUM': 50,
        'HIGH': 200,
        'MIN_TIME_TO_LOG':0,
        'MIN_QUERY_COUNT_TO_LOG':0
    },
    'IGNORE_REQUEST_PATTERNS': [],
    'IGNORE_SQL_PATTERNS': [],
    'DISPLAY_DUPLICATES': None,
}

The QUERYCOUNT['THRESHOLDS'] settings will determine how many queries are interpreted as high or medium (and the color-coded output). In previous versions of this app, this settings was called QUERYCOUNT_THRESHOLDS and that setting is still supported.

The QUERYCOUNT['IGNORE_REQUEST_PATTERNS'] setting allows you to define a list of regexp patterns that get applied to each request's path. If there is a match, the middleware will not be applied to that request. For example, the following setting would bypass the querycount middleware for all requests to the admin:

QUERYCOUNT = {
    'IGNORE_REQUEST_PATTERNS': [r'^/admin/']
}

The QUERYCOUNT['IGNORE_SQL_PATTERNS'] setting allows you to define a list of regexp patterns that ignored to statistic sql query count. For example, the following setting would bypass the querycount middleware for django-silk sql query:

QUERYCOUNT = {
    'IGNORE_SQL_PATTERNS': [r'silk_']
}

New in 0.4.0. The QUERYCOUNT['DISPLAY_DUPLICATES'] setting allows you to control how the most common duplicate queries are displayed. If the setting is None (the default), duplicate queries are not displayed. Otherwise, this should be an integer. For example, the following setting would always print the 5 most duplicated queries:

QUERYCOUNT = {
    'DISPLAY_DUPLICATES': 5,
}

License

This code is distributed under the terms of the MIT license.

Testing

Works only in context of a django installation venv: cd querycount/tests && python test.py

Contributing

Bug fixes and new features are welcome! Fork this project and send a Pull Request to have your work included. Be sure to add yourself to AUTHORS.rst.

django-querycount's People

Contributors

alonisser avatar bradmontgomery avatar essanpupil avatar gitfree avatar gotche avatar

Watchers

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