Django:show / log从python shell的ORM sql调用 [英] Django: show/log ORM sql calls from python shell

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

问题描述

使用优秀的 Django-Devserver ,我发现各种有趣且意外的SQL调用在我的代码我想找到调用来自哪里,所以我正在寻找一种方法来获取Python外壳中由Django ORM生成的所有SQL调用的日志或打印输出。也就是说,当我通过Python shell进行Django ORM调用时,我希望看到打印出来的或打印出来的SQL。



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

解决方案

如果你在shell或任何地方事情,你可以使用查询方法

  query.as_sql()
pre>

打印SQL命令。



ie:

  MyModel.objects.all()。query.as_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.

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

解决方案

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

query.as_sql()

to print the SQL command.

ie:

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

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

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