get方法的查询条件只能有一条数据满足,如果匹配到多条数据都满足,就会报错;如果没有匹配到满足条件的数据,也会报错。
示例代码如下:
from django.http import HttpResponse
from django.db import connection
from .models import Book
def index(request):
book = Book.objects.get(pk=3)
# 而不能是
# book = Book.objects.get(id__gte=3)
print(book)
print(connection.queries)
return HttpResponse("success!")