通过Django调试工具栏捕获SQL查询 [英] Capture SQL queries via Django debug toolbar

查看:237
本文介绍了通过Django调试工具栏捕获SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从命令行调用我的Django应用程序卷曲。我正在通过json的请求,并在json中收集一个响应。



我已经安装了Django调试工具栏。有没有办法可以通过工具栏捕获SQL,并返回与json响应的其余部分?



  @json_response 
def index(request):
try:
ids = json.loads(request.read())[' ids']
除了ValueError:
return HttpResponseBadRequest

listing = MyModel.public()。filter(id__in = [c.split(' - ')[0] for c in $]

prep_list = [l.details(request)for l in listing]

return {'status_code':0,
'status_text' 'success',
'sql_query_list:DjangoDebugToolbar.sql()
'prep_list':prep_list}

任何想法我将替换 DjangoDebugToolbar.sql()

解决方案

尝试这样:

 从django.db导入连接
connection.queries

你可以得到最后一次查询:

  print connection.queries [-1] 

  print connection.queries.pop 


I'm calling my Django application from the command line with curl. I'm passing json in the request and collecting a response in json as well.

I have the Django debug toolbar installed. Is there a way I could capture the SQL via the toolbar and return it with the rest of the json response?

Something like

@json_response
def index(request):
    try:
        ids = json.loads(request.read())['ids']
    except ValueError:
        return HttpResponseBadRequest

    listing = MyModel.public().filter(id__in=[c.split('-')[0] for c in ids])

    prep_list = [ l.details(request) for l in listing ]

    return {'status_code': 0,
            'status_text': 'success',
            'sql_query_list: DjangoDebugToolbar.sql()
            'prep_list': prep_list }

Any idea what I'd put in replacement of DjangoDebugToolbar.sql()?

解决方案

Try this:

from django.db import connection  
connection.queries

you can get de last query doing:

print connection.queries[-1]

or

print connection.queries.pop

这篇关于通过Django调试工具栏捕获SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆