Django Query Analyzer is a Django app that allows you to monitor and analyze the database queries executed by your Django application.
-
Install the
django-query-analyzerpackage using pip:pip install django-query-analyzer
-
Add "django_query_analyzer" to your
INSTALLED_APPSsetting in your Django project'ssettings.py:INSTALLED_APPS = [ ... 'django_query_analyzer', ]
-
Add "django_query_analyzer.middleware.QueryAnalyzerMiddleware" to your project's
MIDDLEWARElist insettings.py:MIDDLEWARE = [ ... "django_query_analyzer.middleware.QueryAnalyzerMiddleware", ]
-
Register the query analyzer URL patterns in your project's
urls.py:from django.urls import path, include urlpatterns = [ ... path("query-analyzer/", include("django_query_analyzer.urls")), ]
-
Start your Django development server:
python manage.py runserver
-
Visit the query analyzer dashboard at http://127.0.0.1:8000/query-analyzer/ in your web browser.
By default, the query analyzer stores the last 50 executed queries. If you want to control this number, you can add the MAX_QUERY_ANALYZER_RECORDS setting to your project's settings.py. For example, to store the last 100 queries, add the following line to your settings.py:
MAX_QUERY_ANALYZER_RECORDS = 100You can control logging to the terminal by adding the ENABLE_LOGGING_TO_TERMINAL setting to your project's settings.py. If set to True, query analysis details will be printed to the terminal. To disable terminal logging, set it to `False.
Example configuration in settings.py:
ENABLE_LOGGING_TO_TERMINAL = TrueYou can control the SQL query to logging the terminal by adding ENABLE_QUERY_LOGGING_ON_CONSOLE to the settings
ENABLE_QUERY_LOGGING_ON_CONSOLE = TrueYou can configure excluded path by configuring the PATHS_TO_EXCLUDE on the settings.py.
by adding the list of path prefixes, you can ignore the execution of query analyzer on the paths. By default query analyzer execute on every paths.
Example configuration in settings.py:
PATHS_TO_EXCLUDE = ['/admin/','/swagger/']