86.QuerySet API常用的方法详解:get方法

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!")
始于才华,忠于颜值;每件事情在成功之前,看起来都是天方夜谭。一无所有,就是无所不能。
原文地址:https://www.cnblogs.com/guyan-2020/p/12271245.html