Capture SQL queries via Django debug toolbar

扶醉桌前 提交于 2019-12-25 02:24:44

问题


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()?


回答1:


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


来源:https://stackoverflow.com/questions/7918751/capture-sql-queries-via-django-debug-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!