1.count():计算数据的个数。
计算数据的个数可以使用count,在python中使用len()也可以计算数据的个数,但是相对来说效率没有使用count()效率高,因为在底层是使用select count(*) 直接在sql层面就计算了数据的个数,而len()是将数据提取到内存中计算数据的个数。因此比使用len函数更高效。
示例代码如下:
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'}]
使用len函数计算book表中数据的多少,示例代码如下:
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'}]
2.first和last:分别是提取数据库表中第一条数据和最后一条数据。
示例代码如下:
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,[email protected],出版社: ->:清华大学出版社)
(书名:精彩极了,页数:333,价格:15.0,打折:3.5,作者:孤烟,20,[email protected],出版社: ->:清华大学出版社)
[{'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'}]
3. exists:判断某条数据是否存在,存在的话就返回True,不存在的话,就返回False。
django底层只查询一次的方式,示例代码如下:
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'}]
使用count判断某条数据是否存在,示例代码如下:
def index(request):
book = Book.objects.filter(name='三国演义').count()
if book > 0:
print("你好这本书还有")
print(connection.queries)
return HttpResponse('success')
django底层执行的sql语句为:
你好这本书还有
[{'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'}]
直接使用if语句进行判断QuerySet,相对来说,这种方法最慢,示例代码如下:
def index(request):
if Book.objects.filter(name='冠岩'):
print("存在")
else:
print("不好意思,这本书不存在")
print(connection.queries)
return HttpResponse('success')
django底层执行的sql语句为:
不好意思,这本书不存在
[{'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更加有效的多。