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!")
相關文章
相關標籤/搜索