查看底层SQL
def showsql():
from django.db import connection
print connection.queries[-1]['sql']
惰性查询
- 延迟查询
- 默认查询前21条数据
- 什么时候需要数据进行查询
from student.models import *
stus = Stu.objects.all()
def showsql():
from django.db import connection
print connection.queries[-1][‘sql’]
showsql()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 3, in showsql
IndexError: list index out of range
Stu.objects.all()
<QuerySet [<Stu: Stu:zhangsan,88>, <Stu: Stu:lisi,90>, <Stu: Stu:wangwu,99>, <Stu: Stu:zhangjie,76>, <Stu: Stu:xiena,99>]>
showsql()
SELECT `t_stu`.`sno`, `t_stu`.`sname`, `t_stu`.`score`, `t_stu`.`created`, `t_stu`.`clazz_id` FROM &#