django-queryinspect

raw JSON →
1.1.0 verified Fri May 01 auth: no python maintenance

Django Query Inspector is a middleware that logs or displays SQL queries executed during a request, including duplicate queries and query counts. Current version 1.1.0, last updated in 2019. No active development, stable for Django 2.x and earlier.

pip install django-queryinspect
error AttributeError: 'NoneType' object has no attribute 'queries'
cause The middleware accesses connection.queries but Django's connection object may be None if no database is configured.
fix
Make sure you have at least one database in DATABASES settings and the connection is established.
error ModuleNotFoundError: No module named 'queryinspect'
cause Import path used is 'django_queryinspect' instead of 'queryinspect'.
fix
Use correct import: 'from queryinspect.middleware import QueryInspectMiddleware'
gotcha The middleware only works with Django's DEBUG=True because it uses django.db.connection.queries, which is only populated when DEBUG is True.
fix Set DEBUG=True in your settings or use a different tool for production profiling.
deprecated This library has not been updated since 2019 and does not officially support Django 3.0+ or Python 3.8+. It may still work with modern Django but is not tested.
fix Consider using django-debug-toolbar or django-silk for active development and support.
gotcha If you use a custom database backend that does not set django.db.connection.queries, the middleware will not capture any queries.
fix Ensure your database backend populates connection.queries (default Django backends do).

Add the middleware to your Django settings. Visit any page and check the console or Django server output for SQL query logs.

MIDDLEWARE = [
    'queryinspect.middleware.QueryInspectMiddleware',
    # ... other middleware
]

# Optional settings
QUERYINSPECT_LOG_TRACEBACKS = False
QUERYINSPECT_ABSOLUTE_LIMIT = 100  # in ms
QUERYINSPECT_LOG_QUERIES = True
QUERYINSPECT_LOG_DUPLICATE_QUERIES = True