from django.http import HttpResponse
from .models import Book
from django.db import connection
def index10(request):
# 计算book表中有多少条数据
book_nums = Book.objects.count()
print(book_nums)
print(connection.queries)
return HttpResponse('success')
7
[{
'sql': 'SELECT @@SQL_AUTO_IS_NULL', 'time': '0.000'},
{
'sql': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'time': '0.000'},
{
'sql': 'SELECT COUNT(*) AS `__count` FROM `book`', 'time': '0.032'}]
def index10(request):
book_nums = Book.objects.all()
print(len(book_nums))
print(connection.queries)
return HttpResponse('success')
7
[07/Feb/2020 08:07:04] "GET / HTTP/1.1" 200 7
[{
'sql': 'SELECT @@SQL_AUTO_IS_NULL', 'time': '0.000'},
{
'sql': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'time': '0.000'},
{
'sql': 'SELECT `book`.`id`, `book`.`name`, `book`.`pages`, `book`.`price`, `book`.`rating`, `book`.`author_id`, `book`.`publisher_id`, `book`.`score` FROM `book`', 'time': '0.000'}]
def index(request):
# 2. first和last分别是提取数据库表中第一条数据和最后一条数据
book_first = Book.objects.first()
book_last = Book.objects.last()
print(book_first)
print(book_last)
print(connection.queries)
return HttpResponse('success')
(书名:三国演义,页数:893,价格:129.0,打折:0.8,作者:罗贯中,47,312587329@qq.com,出版社: ->:清华大学出版社)
(书名:精彩极了,页数:333,价格:15.0,打折:3.5,作者:孤烟,20,7828749204@qq.com,出版社: ->:清华大学出版社)
[{
'sql': 'SELECT @@SQL_AUTO_IS_NULL', 'time': '0.000'},
{
'sql': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'time': '0.000'},
{
'sql': 'SELECT `book`.`id`, `book`.`name`, `book`.`pages`, `book`.`price`, `book`.`rating`, `book`.`author_id`, `book`.`publisher_id`, `book`.`score` FROM `book` ORDER BY `book`.`id` ASC LIMIT 1', 'time': '0.000'}, {
'sql': 'SELECT `book`.`id`, `book`.`name`, `book`.`pages`, `book`.`price`, `book`.`rating`, `book`.`author_id`, `book`.`publisher_id`, `book`.`score` FROM `book` ORDER BY `book`.`id` DESC LIMIT 1', 'time': '0.000'},
{
'sql': 'SELECT `author`.`id`, `author`.`name`, `author`.`age`, `author`.`email` FROM `author` WHERE `author`.`id` = 5 LIMIT 21', 'time': '0.016'},
{
'sql': 'SELECT `publisher`.`id`, `publisher`.`name` FROM `publisher` WHERE `publisher`.`id` = 2 LIMIT 21', 'time': '0.094'},
{
'sql': 'SELECT `author`.`id`, `author`.`name`, `author`.`age`, `author`.`email` FROM `author` WHERE `author`.`id` = 6 LIMIT 21', 'time': '0.000'},
{
'sql': 'SELECT `publisher`.`id`, `publisher`.`name` FROM `publisher` WHERE `publisher`.`id` = 2 LIMIT 21', 'time': '0.000'}]
def index(request):
# 只查询一次的判断方式
book = Book.objects.filter(name='西游记').exists()
print(book)
print(connection.queries)
return HttpResponse('success')
True
[{
'sql': 'SELECT @@SQL_AUTO_IS_NULL', 'time': '0.000'},
{
'sql': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'time': '0.000'},
{
'sql': "SELECT (1) AS `a` FROM `book` WHERE `book`.`name` = '西游记' LIMIT 1", 'time': '0.141'}]
def index(request):
book = Book.objects.filter(name='三国演义').count()
if book > 0:
print("你好这本书还有")
print(connection.queries)
return HttpResponse('success')
你好这本书还有
[{
'sql': 'SELECT @@SQL_AUTO_IS_NULL', 'time': '0.000'},
{
'sql': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'time': '0.000'},
{
'sql': "SELECT COUNT(*) AS `__count` FROM `book` WHERE `book`.`name` = '三国演义'", 'time': '0.000'}]
def index(request):
if Book.objects.filter(name='冠岩'):
print("存在")
else:
print("不好意思,这本书不存在")
print(connection.queries)
return HttpResponse('success')
不好意思,这本书不存在
[{
'sql': 'SELECT @@SQL_AUTO_IS_NULL', 'time': '0.000'},
{
'sql': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'time': '0.000'},
{
'sql': "SELECT `book`.`id`, `book`.`name`, `book`.`pages`, `book`.`price`, `book`.`rating`, `book`.`author_id`, `book`.`publisher_id`, `book`.`score` FROM `book` WHERE `book`.`name` = '冠岩'", 'time': '0.000'}]
总结:判断某个条件的数据是否存在。如果要判断某个条件的元素是否存在,那么使用exists()要比使用count或者是直接使用QuerySet更加有效的多。