{"id":3972,"library":"django-querycount","title":"django-querycount","description":"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.","status":"active","version":"0.8.3","language":"en","source_language":"en","source_url":"https://github.com/bradmontgomery/django-querycount/tarball/0.8.3","tags":["django","performance","debugging","middleware","database","query count","development tool"],"install":[{"cmd":"pip install django-querycount","lang":"bash","label":"Install via pip"}],"dependencies":[{"reason":"Core framework dependency as it's a Django middleware.","package":"Django"}],"imports":[{"symbol":"QueryCountMiddleware","correct":"from querycount.middleware import QueryCountMiddleware"}],"quickstart":{"code":"# myproject/settings.py\n\nimport os\n\n# Ensure DEBUG is True for django-querycount to activate\nDEBUG = os.environ.get('DJANGO_DEBUG', 'True').lower() == 'true'\n\nINSTALLED_APPS = [\n    # ... your Django apps\n]\n\nMIDDLEWARE = [\n    'django.middleware.security.SecurityMiddleware',\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    'django.middleware.common.CommonMiddleware',\n    'django.middleware.csrf.CsrfViewMiddleware',\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    'django.contrib.messages.middleware.MessageMiddleware',\n    'django.middleware.clickjacking.XFrameOptionsMiddleware',\n    'querycount.middleware.QueryCountMiddleware', # Add this line\n    # ... other middleware\n]\n\n# Optional: Customize django-querycount settings\nQUERYCOUNT = {\n    'THRESHOLDS': {\n        'MEDIUM': 50,\n        'HIGH': 200,\n        'MIN_TIME_TO_LOG': 0,\n        'MIN_QUERY_COUNT_TO_LOG': 0\n    },\n    'IGNORE_REQUEST_PATTERNS': [r'^/admin/'], # Example: Ignore admin requests\n    'DISPLAY_DUPLICATES': 5, # Display the 5 most duplicated queries\n    'RESPONSE_HEADER': 'X-DjangoQueryCount-Count' # Custom response header\n}\n\n# Other Django settings...\n","lang":"python","description":"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."},"warnings":[{"fix":"Ensure `DEBUG = True` in your `settings.py` during development where you wish to use django-querycount. Do not enable DEBUG in production environments.","message":"django-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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Migrate your `QUERYCOUNT_THRESHOLDS` setting to `QUERYCOUNT = { 'THRESHOLDS': { ... } }`.","message":"The 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`.","severity":"deprecated","affected_versions":"<=0.8.3"},{"fix":"Remove or comment out `querycount.middleware.QueryCountMiddleware` from `MIDDLEWARE` in production settings, or manage your `DEBUG` setting carefully.","message":"This 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Be 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}