本文介绍了django orm,如何查看(或记录)执行的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法可以打印Django ORM正在生成的查询?说我执行以下语句: Model.objects。过滤器(name ='test')
如何查看生成的SQL查询?
解决方案
每个QuerySet对象都有一个查询
属性,您可以将其记录或打印到stdout进行调试。
qs = Model.objects.filter(name ='test')
print qs.query
修改
ve还使用了自定义模板标签(如所述),以将范围注入范围作为HTML注释的单个请求。
Is there a way I can print the query the Django ORM is generating?
Say I execute the following statement: Model.objects.filter(name='test')
How do I get to see the generated SQL query?
解决方案
Each QuerySet object has a query
attribute that you can log or print to stdout for debugging purposes.
qs = Model.objects.filter(name='test')
print qs.query
Edit
I've also used custom template tags (as outlined in this snippet) to inject the queries in the scope of a single request as HTML comments.
这篇关于django orm,如何查看(或记录)执行的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!