Print django SQL queries in development

2020-11-20

Displaying django SQL queries in development doesn't have to be complicated, just use django-extensions.

This is how you show SQL queries in django shell

manage.py shell_plus --print-sql

This is how you show SQL queries in django runserver

python manage.py runserver_plus --print-sql

If you want to always use this command line option you can set

SHELL_PLUS_PRINT_SQL = True

in settings.py.

By default --print-sql prints only the first 1000 characters of a SQL query. If you want to see more you can increase this limit by setting

# Truncate sql queries to this number of characters
SHELL_PLUS_PRINT_SQL_TRUNCATE = 10_000

in settings.py.

Note: the truncate limit is number of characters, NOT lines.

Also this only works if settings.DEBUG is set to True.

Snippetspythondjangodjango-extensions

PYTEST_CURRENT_TEST environment variable