Django框架ORM之查询函数
![](https://img2022.cnblogs.com/blog/1413093/202207/1413093-20220710204131943-1827117139.png)
from django.shortcuts import render,HttpResponse
from book.models import Book
import datetime
# Create your views here.
def add_book(request):
if request.method == "GET":
return render(request, "add_book.html")
else:
#方式一
#title =request.POST.get("title")
#price = request.POST.get("price")
#pub_date = request.POST.get("pub_date")
#Book.objects.create(title=title,price=price,pub_date=pub_date)
#方式二
#request.POST.dict()
Book.objects.create(**request.POST.dict())
return redirect("/book/select")
#return HttpResponse("添加书籍")
#添加书籍
# 方式1
#date = datetime.date(year=2012,month=12,day=12)
#book = Book(title="xiyouji", price=199, pub_date=date)
book = Book(title="xiyouji",price=199,pub_date="2012-12-12")
print(book.id) #None
book.save() #执行sql
print(book.id) # 3
# 方式2
book = Book.objects.create(title="sanguoyanyi",price=299,pub_date="2011-11-11")
print(book.id)
print(book.price)
def select_book(request):
#return render(request,"add_book.html")
# (1) 查询所有的书籍,返回值是一个queryset类型对象
#book_list= Book.objects.all()
'''
ORM数据引擎:
select id, title, price, pub_date from book:
+------+--------------+---------+------------+
id title price pub_date
+------+--------------+---------+------------+
1 西游记 499.00 2011-12-12
2 三国演义 399.00 2019-06-12
3 水浒传 299.00 2008-06-12
4 红楼梦 199.00 2020-06-12
+------+--------------+---------+------------+
book01 = Book(1 | 西游记 |499.00 |2011-12-12)
book02 = Book(2 | 三国演义 |399.00 |2019-06-12)
book03 = Book(3 | 水浒传 |299.00 |2008-06-12)
book04 = Book(4 | 红楼梦 |199.00 |2020-06-12)
book_list = queryset[book01,book02,book03,book04]
'''
#print ("book_list",book_list) # <QuerySet [<Book: Book object (1)>,...]
#QuerySet:支持索引操作,支持遍历
#book = book_list[0]
#print(book.id) # 1
#print(book.title) # 西游记
#print(book.price) # 499.00
# print(book_list.title)
#for book in book_list:
#print (book.id,book.title) #1 西游记 2 三国演义 3 水浒传 4 红楼梦
#return HttpResponse("添加书籍")
# (2) filter方法: 返回queryset
book_list = Book.objects.filter(price=455) #查看价格为455.00的书籍信息
# (3) exclude方法:排除符合条件的返回queryset类型对象
book_list = Book.objects.exclude(price=455) #排除价格等于455.00的书籍信息
# (4) get方法: 返回值模型类对象 特点: 查询有且只有一个对象时正常返回
book = Book.objects.get(id=7) #查看id为7的书籍信息(唯一一条)
print("book",book)
print("book", book.title,book.price) ##查看id为7的书籍信息的书籍名称和价格(唯一一条)
# (5) first方法,last方法
book = Book.objects.first() # 查询第一条书籍信息
book = Book.objects.last() # 查询最后一条书籍信息
print(book.title)
# (6) queryset.orderby方法:
book_list = Book.objects.all().order_by("-price") # 按价格排除
book_list = Book.objects.all().filter(price_gt=300).order_by("-price") #查询价格大于300.00,并排序
# (7) queryset.count() 计数:
n = book = Book.objects.count()
print (n) # 9
# (8) value(列表.字典),value_list(列表.元组)
#value(列表.字典)
book_list = Book.objects.values("title","price","pub_date")
book_list = Book.objects.filter(price=455).values("title","price","pub_date")
print("book_list", book_list)
#value_list(列表.元组)
book_list2 = Book.objects.filter(price=455).values("title","price","pub_date")
print("book_list2", book_list2)
return render(request,"books.html",{"book_list":book_list})