Registry / aws / django-querycount

django-querycount

JSON →
library0.8.3pypypi✓ verified 25d ago

django-querycount is a Django middleware designed to provide real-time database query monitoring directly in the `runserver` console. It helps developers identify and optimize performance bottlenecks, such as N+1 query problems, by tracking the number of SQL queries executed for each request. This tool is specifically intended for development environments and is hardcoded to operate only when Django's `DEBUG` setting is `True`. The current version is 0.8.3, released in January 2023, and it remains an actively used utility in the Django community for performance debugging.

pip install django-querycount
INSTALL
IMPORT
SIG · DJANGO-QUERYCOUNT
D
django-querycount
awspythonv0.8.3
Install
2.4s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

QueryCountMiddleware
from querycount.middleware import QueryCountMiddleware
from querycount import QueryCountMiddleware

To enable `django-querycount`, add `querycount.middleware.QueryCountMiddleware` to your `MIDDLEWARE` list in your Django project's `settings.py`. Ensure that `DEBUG = True` for `django-querycount` to function. You can also customize its behavior by defining a `QUERYCOUNT` dictionary in your settings.

# myproject/settings.py import os # Ensure DEBUG is True for django-querycount to activate DEBUG = os.environ.get('DJANGO_DEBUG', 'True').lower() == 'true' INSTALLED_APPS = [ # ... your Django apps ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'querycount.middleware.QueryCountMiddleware', # Add this line # ... other middleware ] # Optional: Customize django-querycount settings QUERYCOUNT = { 'THRESHOLDS': { 'MEDIUM': 50, 'HIGH': 200, 'MIN_TIME_TO_LOG': 0, 'MIN_QUERY_COUNT_TO_LOG': 0 }, 'IGNORE_REQUEST_PATTERNS': [r'^/admin/'], # Example: Ignore admin requests 'DISPLAY_DUPLICATES': 5, # Display the 5 most duplicated queries 'RESPONSE_HEADER': 'X-DjangoQueryCount-Count' # Custom response header } # Other Django settings...
Debug
Known issues
gotchadjango-querycount is hardcoded to only run when Django's `DEBUG` setting is `True`. If `DEBUG` is `False`, the middleware will not activate and no query counts will be displayed.
fix
Ensure `DEBUG = True` in your `settings.py` during development where you wish to use django-querycount. Do not enable DEBUG in production environments.
affects: All versions
deprecatedThe old setting `QUERYCOUNT_THRESHOLDS` is still supported but is considered deprecated. The recommended way to configure thresholds and other options is via the `QUERYCOUNT` dictionary in your `settings.py`.
fix
Migrate your `QUERYCOUNT_THRESHOLDS` setting to `QUERYCOUNT = { 'THRESHOLDS': { ... } }`.
affects: <=0.8.3
gotchaThis library is strictly for development and debugging. While it inherently only runs when `DEBUG=True`, ensure it is not relied upon or inadvertently left in production settings, as performance debugging tools can introduce overhead.
fix
Remove or comment out `querycount.middleware.QueryCountMiddleware` from `MIDDLEWARE` in production settings, or manage your `DEBUG` setting carefully.
affects: All versions
gotchaBe aware of other similarly named packages, such as `django-query-counter`. `django-querycount` is specifically a middleware for console output, while `django-query-counter` provides decorators for views and management commands.
fix
Verify the exact package name (`django-querycount`) and the correct import path (`querycount.middleware.QueryCountMiddleware`) to ensure you are using the intended library for middleware-based query counting.
affects: All versions
Upgrade
Version history
0.8.3latest on PyPI · released Jan 5, 2023
Audit
Dependencies
DjangorequiredCore framework dependency as it's a Django middleware.
Agent activity
31 hits · last 30 days
node
26
OpenAI (training)
2
Resources
django-querycount — pip install django-querycount · libregistry