从 Django QuerySet 中获取 SQL [英] Getting the SQL from a Django QuerySet

查看:42
本文介绍了从 Django QuerySet 中获取 SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 QuerySet 对象获取 Django 将在数据库上使用的 SQL?我正在尝试调试一些奇怪的行为,但我不确定哪些查询会进入数据库.

解决方案

您打印查询集的 query 属性.

<预><代码>>>>查询集 = MyModel.objects.all()>>>打印(查询集.查询)SELECT "myapp_mymodel"."id", ... FROM "myapp_mymodel"

How do I get the SQL that Django will use on the database from a QuerySet object? I'm trying to debug some strange behavior, but I'm not sure what queries are going to the database.

解决方案

You print the queryset's query attribute.

>>> queryset = MyModel.objects.all()
>>> print(queryset.query)
SELECT "myapp_mymodel"."id", ... FROM "myapp_mymodel"

这篇关于从 Django QuerySet 中获取 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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