Is there a way to show the SQL that Django is running while performing a query?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
The following returns the query as valid SQL, based on https://code.djangoproject.com/ticket/17741:
If you make sure your settings.py file has:
django.core.context_processors.debug
listed inCONTEXT_PROCESSORS
DEBUG=True
IP
in theINTERNAL_IPS
tupleThen you should have access to the
sql_queries
variable. I append a footer to each page that looks like this:I got the variable
sql_time_sum
by adding the lineto the debug function in django_src/django/core/context_processors.py.
I developed an extension for this purpose, so you can easily put a decorator on your view function and see how many queries are executed.
To install:
To use as context manager:
To use as decorator:
Github: https://github.com/rabbit-aaron/django-print-sql
Take a look at debug_toolbar, it's very useful for debugging.
Documentation and source is available at http://django-debug-toolbar.readthedocs.io/.
I was able to see the failed queries simply by doing:
This assumed that postgres 10.6, ubuntu 18.04+, python3+, django2+, and that logging has been enabled in postgres.
Django-extensions have a command shell_plus with a parameter
print-sql
In django-shell all executed queries will be printed
Ex.: