SQL Log To Console Middleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.db import connection
from django.template import Template, Context
from django.conf import settings

#
# Log all SQL statements direct to the console (when running in DEBUG)
# Intended for use with the django development server.
#

class SQLLogToConsoleMiddleware:
    def process_response(self, request, response): 
        if settings.DEBUG and connection.queries:
            time = sum([float(q['time']) for q in connection.queries])        
            t = Template("{{count}} quer{{count|pluralize:\"y,ies\"}} in {{time}} seconds:\n\n{% for sql in sqllog %}[{{forloop.counter}}] {{sql.time}}s: {{sql.sql|safe}}{% if not forloop.last %}\n\n{% endif %}{% endfor %}")
            print t.render(Context({'sqllog':connection.queries,'count':len(connection.queries),'time':time}))                
        return response

More like this

  1. SQL Log Middleware - with multiple databases by guglielmocelata 3 years, 2 months ago
  2. SQL Log Middleware + duplicates by guettli 6 years, 8 months ago
  3. SQL Log Middleware by joshua 7 years, 1 month ago
  4. SQL Log Middleware w/query count & exec time by tobias 7 years ago
  5. SQL Printing Middleware by ericflo 6 years, 10 months ago

Comments

(Forgotten your password?)