- Author:
- hiddentao
- Posted:
- December 18, 2011
- Language:
- Python
- Version:
- 1.3
- Score:
- 3 (after 3 ratings)
These snippets together give you the ability to view all Django SQL queries executed across all incoming requests by visiting a particular URL (/profiling
in the example). This is useful when developing with the Django test server.
This is useful if most of the incoming requests are AJAX requests, because in such cases the debug toolbar will not be able to show which queries got executed.
The SqlProfilingMiddleware
class is the key one. At the end of every incoming request it appends the executed SQL queries to a static class member list. Any information request profiling information can be added in this way.
The snippet does not add any security around viewing such information. This was done just to keep the code simple. But when implementing this you will definitely want to restrict access to this URL to only people allowed to view such information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | # file: settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'yourapp.middleware.SqlProfilingMiddleware',
...
)
# file: yourapp/midddlware.py
from django.db import connection
import time
class SqlProfilingMiddleware(object):
Queries = []
def process_request(self, request):
return None
def process_view(self, request, view_func, view_args, view_kwargs):
return None
def process_template_response(self, request, response):
self._add_sql_queries(request)
return response
def process_response(self, request, response):
self._add_sql_queries(request)
return response
def process_exception(self, request, exception):
return None
def _add_sql_queries(self, request):
for q in connection.queries:
q["time"] = time.time() + float(q["time"])
SqlProfilingMiddleware.Queries.insert(0, q)
# add request info as a separator
SqlProfilingMiddleware.Queries.insert(0, {"time": time.time(), "path" : request.path})
# file: yourapp/views.py
from yourapp.middleware import SqlProfilingMiddleware
def profiling(request):
return render_to_response("profiling.html", {"queries": SqlProfilingMiddleware.Queries})
# file: urls.py
urlpatterns = patterns('',
(r'^profiling/$', 'yourapp.views.profiling'),
)
# file: yourapp/templates/profiling.html
<div id="profiling">
<h2>SQL queries</h2>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Time</th>
<th>SQL</th>
</tr>
</thead>
<tbody>
{% for q in queries %}
{% if q.path %}
<tr class="path">
<td >{{ q.time }}</td>
<td>{{ q.path }}</td>
</tr>
{% else %}
<tr class="sql">
<td >{{ q.time }}</td>
<td>{{ q.sql }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
This snippet could be great app.
#
Please login first before commenting.