你可以写一个中间件用于此目的:
from django.db import connection
class SqlPrintMiddleware(object):
def process_response(self, request, response):
sqltime = 0 # Variable to store execution time
for query in connection.queries:
sqltime += float(query["time"]) # Add the time that the query took to the total
# len(connection.queries) = total number of queries
print "Page render: " + unicode(sqltime) + "sec for " + unicode(len(connection.queries)) + " queries"
return response
而在你的settings.py变化:
MIDDLEWARE_CLASSES = (
# ...
'your_app.middleware.SqlPrintMiddleware',
# ...
)
理念采取从here