多表查询代码

   import os

    if __name__ == '__main__':
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day82.settings")
        import django

        django.setup()
        from app01 import models

        # 一对多增加
        # publish:可以传一个publish对象
        # publish=models.Publish.objects.get(pk=1)
        # print(publish.name)
        # # ret=models.Book.objects.create(name='西游记',price=88,publish_date='2018-09-12',publish=publish)
        # # publish_id 传一个id
        # ret=models.Book.objects.create(name='三国演义',price=32,publish_date='2018-07-12',publish_id=publish.pk)
        # print(type(ret.publish))
        # # 就是当前图书出版社的对象
        # print(ret.publish)
        # print(type(ret.publish_id))
        # print(ret.publish.pk)
        # print(ret.publish_id)
        #     一对多修改
        #     book=models.Book.objects.get(pk=3)
        #     book.publish_id=2
        #     # book.publish=出版社对象
        #     book.save()
        # ret=models.Book.objects.filter(pk=2).update(publish=publish对象)
        # ret=models.Book.objects.filter(pk=2).update(publish_id=2)

        # 一对一增加
        # authordetail=models.AuthorDatail.objects.create(addr='南京')
        # # author=models.Author.objects.create(name='恩公',age=17,author_detail=authordetail)
        # author=models.Author.objects.create(name='小猴',age=16,author_detail_id=authordetail.pk)

        # 多对多新增
        # 给红楼梦这本书添加两个作者(lqz,egon)
        book = models.Book.objects.get(pk=1)
        # 相当于拿到了第三张表
        # 往第三章表中添加纪录(问题来了?要传对象还是传id),都支持
        # book.authors.add(1,2)
        lqz = models.Author.objects.get(pk=1)
        egon = models.Author.objects.get(pk=2)
        # book.authors.add(lqz,egon)
        # 红楼梦这本书egon这个作者删掉
        # book.authors.remove(2)
        # book.authors.remove(egon,lqz)
        # book.authors.remove(1,2)
        # book.authors.remove(*[1,2])
        # book.authors.remove(*[lqz,egon])
        #     修改红楼梦这本书的作者为lqz和egon
        #     清空(清空这本的所有作者记录)
        #     book.authors.clear()
        #     book.authors.add(1,2)
        #     book.authors.set(*[6,])   #这样不行
        #     book.authors.set([6,])   #需要这样传
        #     lqz=models.Author.objects.get(pk=2)
        # set 必须传一个可迭代对象
        # book.authors.set([lqz,])   #需要这样传
        # 一对一查询
        # 补充一个概念:正向  反向
        # 正向:关联关系在当前表中,从当前表去另一个表
        # 反向:关联关系不在当前表,从当前表去另一个表
        # 查询lqz作者的地址(正向查询,按字段)
        # lqz=models.Author.objects.filter(name='lqz').first()
        # # 作者详情对象
        # print(lqz.author_detail.addr)

        # 查询地址为上海的,作者的名字(反向查询,按表名小写)
        # authordetail=models.AuthorDatail.objects.filter(addr='上海').first()
        # 拿到的是作者对象authordetail.author
        # print(authordetail.author.name)

        # 一对多
        # 查询红楼梦这本书的出版社名字(正向,按字段)
        # book=models.Book.objects.get(pk=1)
        # 出版社对象 book.publish
        # print(book.publish.name)
        # 查询北京出版社出版的所有书名(反向查询按 表名小写_set.all())
        # publish=models.Publish.objects.get(pk=1)
        # 结果是queryset对象
        # books=publish.book_set.all()
        # for book in books:
        #     print(book.name)
        # 查询以红开头的
        # books=publish.book_set.all().filter(name__startswith='')
        # for book in books:
        #     print(book.name)
        # 多对多
        # 红楼梦这本书所有的作者(正向 字段)
        # book=models.Book.objects.get(pk=1)
        # # book.authors.all()拿到所有的作者,是一个queryset对象
        # authors=book.authors.all()
        # for author in authors:
        #     print(author.name)
        # 查询egon写的所有书(反向 表名小写_set.all())
        # egon=models.Author.objects.get(pk=2)
        # 拿到的是queryset对象
        # books=egon.book_set.all()
        # for book in books:
        # print(book.name)
        # 基于双下滑线的跨表查询
        # 一对一
        # 查询lqz作者的名字,地址(正向查询,按字段)
        # ret=models.Author.objects.filter(name='lqz').values('name','author_detail__addr')
        # print(ret)
        # 查询地址为上海的作者的名字(反向,按表名小写)
        # ret=models.AuthorDatail.objects.filter(addr='上海').values('addr','author__name','author__age')
        # print(ret.query)
        # print(ret)
        # 一对多
        # 查询红楼梦这本书的出版社的名字(正向  按字段)
        # ret=models.Book.objects.filter(name='红楼梦').values('name','publish__name')
        # print(ret)
        # 查询北京出版社出版的所有书的名字(反向  按表名小写)
        # ret=models.Publish.objects.filter(name='北京出版社').values('book__name')
        # print(ret)
        # 多对多
        # 红楼梦这本书所有的作者名字(正向  按字段)
        # ret=models.Author.objects.filter(book__name='红楼梦').values('name')
        # print(ret)

        # ret=models.Book.objects.filter(name='红楼梦').values('authors__name')
        # print(ret)
        # egon出版的所有书的名字(反向 表名小写)
        # ret=models.Book.objects.filter(authors__name='egon').values('name')
        # print(ret)
        # ret=models.Author.objects.filter(name='egon').values('book__name')
        # print(ret)
        # 查询北京出版社出版过的所有书籍的名字以及作者的姓名
        # ret=models.Publish.objects.filter(name='北京出版社').values('book__name','book__authors__name')
        # print(ret)
        # ret=models.Book.objects.filter(publish__name='北京出版社').values('name','authors__name')
        # print(ret)
        # ret=models.Author.objects.filter(book__publish__name='北京出版社').values('book__name','name')
        # print(ret)
        # 地址是以北开头的作者出版过的所有书籍名称以及出版社名称
        # ret = models.AuthorDatail.objects.filter(addr__startswith='').values('author__book__name',
        #                                                                       'author__book__publish__name')
        # print(ret)

        ret = models.Book.objects.filter(authors__author_detail__addr__startswith='').values('name', 'publish__name')

        print(ret.query)
        ret = models.Author.objects.filter(author_detail__addr__startswith='').values('book__name',
                                                                                       'book__publish__name')
        print(ret.query)

 

猜你喜欢

转载自www.cnblogs.com/zhouhai007/p/10269152.html
0条评论
添加一条新回复