• Stars
    star
    383
  • Rank 111,325 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Middleware that Prints the number of DB queries to the runserver console.

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.

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,
    'RESPONSE_HEADER': 'X-DjangoQueryCount-Count'
}

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_']
}

The QUERYCOUNT['RESPONSE_HEADER'] setting allows you to define a custom response header that contains the total number of queries executed. To disable this header, the supply None as the value:

QUERYCOUNT = {
    'RESPONSE_HEADER': None
}

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

Run python manage.py test querycount to run the tests. Note that this will modify your settings so that your project is in DEBUG mode for the duration of the querycount tests.

(side-note: this project needs better tests; for the moment, there are only smoke tests that set up the middleware and call two simple test views).

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.

More Repositories

1

django-redis-metrics

Metrics for django apps backed by Redis.
Python
94
star
2

python-laser-tracker

A simple laser tracker using Python and OpenCV.
Python
82
star
3

word2html

a quick and dirty script to convert a Word (docx) document to html.
Python
52
star
4

django-rainbowtests

This is a custom test runner for Django that gives you *really* colorful test output.
Python
43
star
5

django-staticflatpages

This is an app sort of like Django's contrib.flatpages, but without the database. It's got a Fallback Middleware that just serves static html documents from your filesystem.
Python
25
star
6

alfred-percent-change

An Alfred workflow to help you do percentage calculations
Python
22
star
7

pgSlideShow

A small image slideshow app written in python using pygame.
Python
11
star
8

correlation

A python module with functions to compute two images' Correlation Coefficients.
Python
8
star
9

dotfiles

a few of my dotfiles
Vim Script
7
star
10

stupid-simple-php-app-for-fabric-demo

This is a repo used to demo Fabric (http://fabfile.org)
7
star
11

shaney

A copy of of the Mark V. Shaney markov chain.
Python
6
star
12

django-janitor

django-janitor allows you to use bleach to clean HTML stored in a Model's field.
Python
6
star
13

elasticdict

A python dictionary-like object that puts it's data in elasticsearch
Python
5
star
14

django-commen5

Gives you a {% commen5 %} Django template tag which can be used in place of {% comment %}.
Python
5
star
15

zerochat

a stupid simple command-line chat server and client using zeromq
Python
4
star
16

tn_lottery

Generates numbers for the Tennessee Lottery games and includes a simple powerball simulation.
Python
4
star
17

mempy-flask-tutorial

A simple flask tutorial given at the Memphis Python User group.
Python
4
star
18

django-blargg

because the world needs another django-powered blog app!
Python
4
star
19

django-chosenadmin

Adds Chosen.js to the Django Admin app.
Python
3
star
20

python-ninethreesix

password generation according to xkcd 936
Python
3
star
21

swift_wrapper

Some bash functions that make using swift (python cli for OpenStack / Rackspace CloudFiles) less verbose.
Shell
3
star
22

restful_api_example

A very simple RESTful api in flask.
Python
2
star
23

demo-project

A demo React project for Tech901
JavaScript
1
star
24

mempy-django-walkthru

This repo contains a sample Django app and an introductory, walk-thru presentation given at the Memphis Python User Group.
Python
1
star
25

demo-server

a simple express-based api for Tech901's Web Programming course
JavaScript
1
star
26

pycropper

Quick cropping for a large collection of images. Built with pygame.
Python
1
star
27

python-twitter-filter

Python
1
star
28

pillow-walkthru

A quick and easy walkthru tutorial for some of the things you can do with Pillow. This is talk for MEMpy Dec 2015.
Python
1
star
29

echyo

Auto-replies to twitter mentions with a Yo
Python
1
star
30

faviconize

A jQuery plugin for displaying a favicon on an external link. Originally by Samuel Le Morvan.
JavaScript
1
star
31

ctypes_example

A stupid-simple contrived example of using ctypes to re-write some python code in C. (built for MEMpy, Oct 2015)
Python
1
star
32

st-jude-marathon

Analyzing the results of the St Jude Memphis Marathon
HTML
1
star