Logowanie zapytań SQL w Django

Na djangosnippets znajdziemy kod middleware zapisującego wykonane zapytania dla danego widoku.
from django.db import connection
from django.template import Template, Context

class SQLLogMiddleware:

    def process_response ( self, request, response ): 
        time = 0.0
        for q in connection.queries:
                time += float(q['time'])
        
        t = Template('''
            <p><em>Total query count:</em> {{ count }}<br/>
            <em>Total execution time:</em> {{ time }}</p>
            <ul class="sqllog">
                {% for sql in sqllog %}
                    <li>{{ sql.time }}: {{ sql.sql }}</li>
                {% endfor %}
            </ul>
        ''')
Middleware zapisujący wykonane zapytania
        response.content = "%s%s" % ( response.content, t.render(Context({'sqllog':connection.queries,'count':len(connection.queries),'time':time})))
        return response
Podany we fragmencie kod zapisujemy w pliku w katalogu projektu django (lub doklejamy metodę do istniejącego middleware) i dodajemy do MIDDLEWARE_CLASSES w settings.py. Teraz wystarczy w szablonie umieścić tag {{ sqllog }} by wyświetlić listę wykonanych zapytań i czas ich wykonania. Przykładowo u mnie wyglądało to tak:
Total query count: 9
 Total execution time: 0.013
0.003: SELECT COUNT(*) FROM "stats_stat" WHERE ("stats_stat"."date" = 2007-05-17 AND "stats_stat"."ip" = 127.0.0.1)
0.001: SELECT "django_session"."session_key","django_session"."session_data","django_session"."expire_date" FROM "django_session" WHERE ("django_session"."session_key" = *** AND "django_session"."expire_date" > 2007-05-17 20:09:39.287943)
0.003: SELECT "rk_topic1"."id","rk_topic1"."topic_name","rk_topic1"."topic_last_pagination_page","rk_topic1"."topic_lastpost" FROM "rk_topic1" ORDER BY "rk_topic1"."topic_modification_date" DESC LIMIT 4 
0.001: SELECT "rk_page1"."id","rk_page1"."title","rk_page1"."slug","rk_page1"."description","rk_page1"."text","rk_page1"."date","rk_page1"."is_category","rk_page1"."wykop_field","rk_page1"."infoneo_field","rk_page1"."jakilinux_field","rk_page1"."digg_field","rk_page1"."reddit_field","rk_page1"."parent_id" FROM "rk_page1" WHERE ("rk_page1"."slug" = index)
0.001: SELECT "rk_com1"."id","rk_com1"."text","rk_com1"."date","rk_com1"."apptype","rk_com1"."appid","rk_com1"."author_id","rk_com1"."ip" FROM "rk_com1" ORDER BY "rk_com1"."id" DESC LIMIT 4 
0.002: SELECT "rk_news1"."id","rk_news1"."title","rk_news1"."slug","rk_news1"."text","rk_news1"."text_more","rk_news1"."is_more","rk_news1"."is_external","rk_news1"."category_id","rk_news1"."date","rk_news1"."jakilinux_field","rk_news1"."wykop_field","rk_news1"."infoneo_field","rk_news1"."digg_field","rk_news1"."reddit_field","rk_ncat1"."id","rk_ncat1"."title","rk_ncat1"."slug","rk_ncat1"."icon" FROM "rk_news1" , "rk_ncat1" WHERE "rk_news1"."category_id" = "rk_ncat1"."id" ORDER BY "rk_news1"."id" DESC LIMIT 5 
0.000: SELECT "rk_page1"."slug","rk_page1"."title" FROM "rk_page1" ORDER BY "rk_page1"."id" DESC LIMIT 6 
0.001: SELECT "rk_task1"."id","rk_task1"."name" FROM "rk_task1" WHERE ("rk_task1"."status" = True) ORDER BY "rk_task1"."modification_date" DESC LIMIT 4 
0.001: SELECT "userpanel_profile"."id","userpanel_profile"."username_id","userpanel_profile"."onsitedata","userpanel_profile"."use_messages" FROM "userpanel_profile" WHERE ("userpanel_profile"."onsitedata" > 2007-05-17 19:09:39.296164) ORDER BY "userpanel_profile"."onsitedata" DESC LIMIT 5 
RkBlog

Django, 14 July 2008

Comment article
Comment article RkBlog main page Search RSS Contact