django-models 数据库取值

发布时间:2019-03-28 20:42:41编辑:auto阅读(1898)

      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()

     

关键字