Django:显示/记录来自 python shell 的 ORM sql 调用 [英] Django: show/log ORM sql calls from python shell

查看:20
本文介绍了Django:显示/记录来自 python shell 的 ORM sql 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用优秀的 Django-Devserver 我发现了各种有趣和意想不到的 SQL 调用在我的代码中.我想找到调用的来源,因此我正在寻找一种方法来获取 Python shell 中 Django ORM 生成的所有 SQL 调用的日志或打印输出.也就是说,当我通过 Python shell 执行 Django ORM 调用时,我希望看到打印出或记录的结果 SQL.

Using the excellent Django-Devserver I'm finding all kinds of interesting and unexpected SQL calls in my code. I wanted to find where the calls are coming from, and so I'm looking for a way to get a log or print-out of all SQL calls generated by the Django ORM in the Python shell. That is, when I do a Django ORM call via the Python shell, I'd like to see the resulting SQL printed out or logged.

我注意到几个将日志信息添加到 html 页面的解决方案.有没有一种简单的方法可以转储到命令行?

I noticed several solutions that add log info to the html page. Is there an easy way to dump to the command line instead?

推荐答案

如果你在 shell 中,或者在任何地方,你可以使用 queryset 方法

If you're in the shell, or anywhere for that matter, you can use the queryset method

query.as_sql()

打印 SQL 命令.

即:

MyModel.objects.all().query.as_sql()

这篇关于Django:显示/记录来自 python shell 的 ORM sql 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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