django-models 資料庫取值
阿新 • • 發佈:2018-11-03
1 django.shortcuts import render,HttpResponse 2 from app01.models import * 3 # Create your views here. 4 5 def index(request): 6 return render(request,"index.html") 7 8 def addbook(request): 9 # 一對多的新增 10 11 12 13 # 第一種方式 14 # Book.objects.create(name="Linux運維",price=1133,pub_date="2017-11-22",publish_id=2)15 16 17 18 # 第二種新增方式 publish_id,沒辦法新增publish_obj 19 # publish_obj=Publish.objects.filter(name="人民出版社")[0] 20 # print("+++!!!****",publish_obj) 21 # +++!!!**** Publish object (1) 22 # print(type(publish_obj)) 23 # <class 'app01.models.Publish'> 24 25 # Book.objects.create(name="GO語言", price=43, pub_date="2017-07-17", publish_id=2)26 # INSERT INTO `app01_book` (`name`, `price`, `pub_date`, `publis 27 # h_id`) VALUES ('GO語言', 43, '2017-07-17', 2); args=['GO語言', 43, '20 28 # 17-07-17', 2] 29 30 31 32 # 拿到資料庫裡面的表的值,有兩步,先拿到值,再賦給物件 33 # 再從裡面取值 34 # 先拿到book表,取到name=python的值,再賦給物件 35 # 如果是兩個值,會報錯,get只能得到一個值36 # book_obj=Book.objects.get(name='python') 37 # 38 # print("----",book_obj) 39 40 41 42 # 一對多:book_obj.publish------一定是一個物件 43 # 如果是要取外來鍵的表的值,取publish會取到一個publish物件 44 # print(type(book_obj.publish)) 45 # print(book_obj.publish.name) 46 # print(book_obj.publish.city) 47 48 49 50 # 從子表去找主表 51 # 要拿到表中外來鍵的表的值,步驟有 52 # 先拿到publish表是name的值等於機械出版社的一整行物件pub_obj(表中有id,name,city) 53 # SELECT `app01_publish`.`id`, `app01_publish`.`name`, `app01_pu 54 # blish`.`city` FROM `app01_publish` WHERE `app01_publish`.`name` = '機 55 # 械出版社'; args=('機械出版社',) 56 # 把pub_obj結果賦值給publish,由id作為查詢依據,再從book表中找到要查詢的name跟price 57 # SELECT `app01_book`.`name`, `app01_book`.`price` FROM `app01_b 58 # ook` WHERE `app01_book`.`publish_id` = 4 LIMIT 21; args=(4,) 59 60 # get查詢方式:表.objects.get()--只能有一個物件 61 # pub_obj=Publish.objects.get(name='機械出版社') 62 63 # filter查詢方式:表.objects.filter()[0]--是物件的集 64 # pub_obj2=Publish.objects.filter(name="山西出版社")[0] 65 # print(pub_obj) 66 # Publish object (4) 67 68 # print(pub_obj.city) 69 # 上海 70 # print(pub_obj.id) 71 # 4 72 73 # 關鍵點:把publish表的物件放進filter裡面篩選,再從裡面values取值 74 # ret=Book.objects.filter(publish=pub_obj).values("name","price") 75 # ret2=Book.objects.filter(publish=pub_obj2).values("name","pub_date") 76 77 # print(ret) 78 # print(ret2) 79 # 機械出版社出版的書籍跟價格---query物件 80 # <QuerySet [{'name': 'Java', 'price': 65}, {'name': 'Linux開發', 'price 81 # ': 1133}, {'name': 'Linux系統', 'price': 133}, {'name': 'Linux伺服器', 82 # 'price': 133}]> 83 84 85 86 # 從主表再去找子表 87 # ,第一步相同,從publish表找到name等於南方出版社,賦值給pub_obj 88 pub_obj = Publish.objects.filter(name='山西出版社')[0] 89 # 再從pub_obj裡面找到book_set.all()(book子表的所有內容) 是一個物件集合 90 91 # publish表拿到了book表的物件,表名+單引號,得到了表名裡面的queryset物件.all()的所有值, 92 # book_set.all(),再從裡面values取值 93 print(pub_obj.book_set.all()) 94 # <QuerySet [<Book: Django>, <Book: Linux運維>, <Book: sqlyog>, <Book: Linux程式碼>, <Book: GO語言>, <Book: GO語言>, <Book: GO語言>, <Book: moring>]> 95 96 print(pub_obj.book_set.all().values("name",'price')) 97 # <QuerySet [{'name': 'Django', 'price': 45}, {'name': 'Linux運維', 'pri 98 # ce': 1133}, {'name': 'sqlyog', 'price': 1133}, {'name': 'Linux程式碼', ' 99 # price': 133}, {'name': 'GO語言', 'price': 43}, {'name': 'GO語言', 'pri 100 # ce': 43}, {'name': 'GO語言', 'price': 43}, {'name': 'moring', 'price': 101 # 23}]> 102 103 # 更簡便的第3種方式: 104 ret=Book.objects.filter()