欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

django-models 数据库取值

程序员文章站 2024-02-06 20:06:19
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... ......
  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()