django 如何查看orm转换成的sql语句

问题:

1.django的ORM语句和原生sql语句大相径庭,在项目开发中如何确保写的ORM语句与自己想要的sql语句师一直的呢???

解决方案:

方案1:

当是查询语句且查询结果是QuerySet对象时,可以使用QuerySet的query属性查看转化成的sql语句,如下:

newses = NewsModel.objects.select_related('category','author').all()[0:2]
print(newses.query)
print('---'*25)
print(NewsModel.objects.all().query)
print('---'*25)
print(NewsModel.objects.filter(id__gt=2)[3:5].query)

# 打印结果:
SELECT `news_newsmodel`.`id`, `news_newsmodel`.`title`, `news_newsmodel`.`desc`, `news_newsmodel`.`thumbnail`, `news_newsmodel`.`publish_time`, `news_newsmodel`.`content`, `news_newsmodel`.`category_id`, `news_newsmodel`.`author_id`, `cms_newscategorymodels`.`id`, `cms_newscategorymodels`.`name`, `authPro_user`.`id`, `authPro_user`.`password`, `authPro_user`.`last_login`, `authPro_user`.`is_superuser`, `authPro_user`.`telephone`, `authPro_user`.`username`, `authPro_user`.`email`, `authPro_user`.`is_active`, `authPro_user`.`gender`, `authPro_user`.`date_joined`, `authPro_user`.`is_staff` FROM `news_newsmodel` LEFT OUTER JOIN `cms_newscategorymodels` ON (`news_newsmodel`.`category_id` = `cms_newscategorymodels`.`id`) LEFT OUTER JOIN `authPro_user` ON (`news_newsmodel`.`author_id` = `authPro_user`.`id`) ORDER BY `news_newsmodel`.`publish_time` DESC  LIMIT 2
---------------------------------------------------------------------------
SELECT `news_newsmodel`.`id`, `news_newsmodel`.`title`, `news_newsmodel`.`desc`, `news_newsmodel`.`thumbnail`, `news_newsmodel`.`publish_time`, `news_newsmodel`.`content`, `news_newsmodel`.`category_id`, `news_newsmodel`.`author_id` FROM `news_newsmodel` ORDER BY `news_newsmodel`.`publish_time` DESC
---------------------------------------------------------------------------
SELECT `news_newsmodel`.`id`, `news_newsmodel`.`title`, `news_newsmodel`.`desc`, `news_newsmodel`.`thumbnail`, `news_newsmodel`.`publish_time`, `news_newsmodel`.`content`, `news_newsmodel`.`category_id`, `news_newsmodel`.`author_id` FROM `news_newsmodel` WHERE `news_newsmodel`.`id` > 2 ORDER BY `news_newsmodel`.`publish_time` DESC  LIMIT 2 OFFSET 3

方案2:

直接在项目中使用原生sql语句对数据库进行操作,python借助pymysql模块操作mysql的简单流程如下:

# 导入pymysql模块
import pymysql

#定义配置信息(字典形式)
db_config = {
  'host' : '127.0.0.1',
  'user' : 'xxx',      #数据库连接用户
  'password' : 'xxx',    #数据库连接密码
  'db' : 'tan'      #数据库名
}


# 建立连接
conn = pymysql.connect(**db_config)
# 建立游标
cursor = conn.cursor()

# 执行sql语句:  cursor.excute("原生sql语句")  注意:此处的原生sql语句结尾不用加;
cursor.excute("select * from students")
#获取数据(列表形式),此处需主动获取数据
values = cursor.fetchall()
# 打印数据
for value in values:
      print(value)

#提交操作
conn.commit()
#游标关闭
cursor.close()
#连接关闭
conn.close()

方案3:

django框架采用的ORM模型,我们可以通过mysql的日志记录实时查看执行的sql语句,具体步骤如下:

第一步:进入mysql,查看日志开启的状态和log文件路径;

mysql>  show variables like "%general_log%";
+------------------+-------------------------+
| Variable_name    | Value                   |
+-------------------+------------------------+
| general_log      | OFF                     |
| general_log_file | /var/lib/mysql/VIP.log  |
+------------------+-------------------------+
2 rows in set (0.00 sec)

第二步:如上操作,OFF说明没有开启日志记录,我们可以通过如下命令设置日志启动状态 or 更改日志路径和日志名;

mysql> set global general_log_file = '/var/lib/mysql/localhost.log';

mysql> set global general_log = 'ON';

mysql>  show variables like "%general_log%";
+------------------+------------------------------+
| Variable_name    | Value                        |
+------------------+------------------------------+
| general_log      | ON                           |
| general_log_file | /var/lib/mysql/localhost.log |
+------------------+------------------------------+
2 rows in set (0.00 sec)



!!!注意:

日志开启后,所有执行的sql都会被记录下来,但是如果重启mysql就会停止记录,即general_log的值变回OFF!

/****** 更改general_log='ON'后若重启mysql服务  *****/

# 重启mysql服务
service mysql restart

# 进入mysql再次查看日志状态,general_log再次变为OFF
mysql>  show variables like "%general_log%";
+------------------+------------------------+
| Variable_name    | Value                  |
+------------------+------------------------+
| general_log      | OFF                    |
| general_log_file | /var/lib/mysql/VIP.log |
+------------------+------------------------+
2 rows in set (0.00 sec)

拓展:
mysql数据库支持两种日志存储方式:文件(file) and 数据表(table)

# 查询mysql日志存储方式
mysql> show variables like "%log_output%";

# 设置mysql日志存储方式:文件存储方式(默认方式)
set global log_output = "FILE";

# 设置mysql日志存储方式:数据表存储方式
set global log_output = "TABLE";

# 设置mysql日志存储方式:文件和数据表同时存储日志
set global log_output = "TABLE,FILE";

你可能感兴趣的:(django 如何查看orm转换成的sql语句)