I know, that in settings, I can have debug = True and all SQL queries are logged.
But, I want to log all the SQL queries made by one specific view, before the response is returned.
How can I do this in Django 1.3 ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This can be done by changing logging settings within context of specific view.
You still need to have
DEBUGenabled:… but you can modify configuration so that only queries by specific view(s) are logged.
Here’s how it could be done:
1: In
settings.pyset django SQL logger (django.db.backends) level toINFOor higher and verify that it stopped recording SQL queries.2.a. First line in your view to have SQL logging, set that logger level to
DEBUG. Last line, set it back to original value. This is simplest way, but queries executed by middleware before/after view code don’t get logged.2.b. Write custom middleware to do the same thing before and after view processing. If you place it first, all queries made by other middlewares will be also logged.
Note that this method is not thread safe, but since you only want this setup in development environment, this should not matter.
Django documentation “Logging” chapter:
https://docs.djangoproject.com/en/1.4/topics/logging/
Python logging library reference:
http://docs.python.org/library/logging.html