かぴぶろぐ

またかぴったかと思った・・・(´A`;)

Django runserverにSQLクエリを吐かせる方法

カテゴリ[ django ]
まず、approotにdebugディレクトリを作り、その中にdebug用pyを作る。
> mkdir debug
> cd debug
> touch __init__.py
> vi db.py
[db.py]
from django.conf import settings
from django.db import connection

class DBDebugMiddleware:
"""
"DBDebug" middleware for debug out O/R Mapper's SQL:
"""

def process_response(self, request, response):
if settings.DEBUG :
for query in connection.queries:
print "---query----------------------------------"
print "Time:\n %s \n SQL:\n %s" % (query['time'], query['sql'])
print "---query---end----------------------------"

return response
settings.pyに読み込ませる
[settings.py]
MIDDLEWARE_CLASSES = (
  [その他]
'approot.debug.db.DBDebugMiddleware',
)
実際の表示はこんな感じ
[root@localhost site_manager]# python24 manage.py runserver 0.0.0.0:8000
Validating models...

0 errors found

Django version 0.97-pre-SVN-unknown, using settings 'site_manager.settings'
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[24/Sep/2007 01:19:01] "GET / HTTP/1.1" 404 1917
[24/Sep/2007 01:19:03] "GET /admin HTTP/1.1" 301 0
[24/Sep/2007 01:19:03] "GET /admin/ HTTP/1.1" 200 6590
Validating models...

0 errors found

Django version 0.97-pre-SVN-unknown, using settings 'site_manager.settings'
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.


---query----------------------------------
Time:
0.001
SQL:
SELECT `django_content_type`.`id`,`django_content_type`.`name`,`django_content_type`.`app_label`,`django_content_type`.`model` FROM `django_content_type` WHERE (`django_content_type`.`id` = 12)
---query---end----------------------------
---query----------------------------------
Time:
0.002
SQL:
SELECT `django_content_type`.`id`,`django_content_type`.`name`,`django_content_type`.`app_label`,`django_content_type`.`model` FROM `django_content_type` WHERE (`django_content_type`.`id` = 9)
---query---end----------------------------
[24/Sep/2007 01:19:17] "GET /admin/ HTTP/1.1" 200 6590

http://kapi.jp/kapi_blog/86

2007年11月22日

関連カテゴリ django

この記事のコメント

この記事にコメントする