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

get方法的查询条件只能有一条数据知足,若是匹配到多条数据都知足,就会报错;若是没有匹配到知足条件的数据,也会报错。

示例代码以下:python

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!")
相关文章
相关标签/搜索