惰性机制:Publisher.objects.all()或者.filter()等都只是返回了一个QuerySet(查询结果集对象)【https://www.cnblogs.com/chaojiyingxiong/p/9703609.html 】,而且他并不会马上执行SQL,而是当调用QuerySet的时候才执行SQL,为了测试,我们的加上SQL的日志

载views.py上修改

浏览器访问http://127.0.0.1:8000/data_oper/结果显示SQL语句没有执行
调用QuerySet,载views.py上修改

rom django.shortcuts import render, HttpResponse from app01 import models from app01.models import Book,Author,Publisher # Create your views here. def data_oper(req): obj_set = models.Book.objects.filter(id=2) for obj in obj_set: print(obj.title) return HttpResponse("Hello world")
浏览器访问http://127.0.0.1:8000/data_oper/结果显示SQL语句执行