CSS Ribbon

Reproducing the GitHub Ribbon in CSS

路飞学城Python-Day101

57-多表操作之一对多添加纪录
def add(request):
# pub = Publish.objects.create(name='人民出版社', email='873245193@qq.com', city='扬州')
# 为book表绑定关系,绑定和publish表的关系
# ------------------方式一和方式二都是绑定一对多的关系
# 方式一
# book_obj = Book.objects.create(title='金瓶', price=100, publishDate="2018-10-05", publish_id=1)
# print(book_obj)
# 方式二 找到实例的对象后直接赋值
# pub_obj = Publish.objects.filter(nid=1).first()
# book_obj = Book.objects.create(title='三体2', price=996, publishDate="2018-10-03", publish=pub_obj)
# print(book_obj.title)
# print(book_obj.publish.name)
# -----------------------------------
# 查询西游记的出版社对应的邮箱
# book_obj = Book.objects.filter(title="西游记").first()
# book_obj_pub = book_obj.publish.email
# print(book_obj_pub)
# +-------------------------------+
# 建立多对多的关系
# book_obj = Book.objects.create(title='暗网', price=100, publishDate="2018-10-06", publish_id=1)
# boy = Author.objects.get(nid=2)
# panda = Author.objects.get(nid=1)
# 多对多关系使用的是many2many的操作的话是不能直接操作表的记录的,必须通过Django的接口去做
# book_obj.authors.add(boy, panda)
# +------其他玩法-------+
# book_obj.authors.add(1, 2, 3) = book_obj.authors.add(*[1, 2, 3])
# 解除多对多的关系(首先必须有多对多的关系)
# book = Book.objects.filter(nid=8).first()
# book.authors.remove(2)
# 清除全部
# book.authors.clear()
return HttpResponse('OK')
 
 
58-多表操作之多对多添加纪录

59-基于对象的跨表查询简介
跨表查询比单表查询要多得多,一般表很少有单表,更多的都是多表之间互相关联的关系
ORM中的查询是最复杂也最难实现的地方,需要多注意
60-基于对象跨表查询之一对多
使用什么方式的查询都需要视情况而定,本身是需要灵活应用的
# 1.基于对象查询
# 查询一本书的出版社的名字
# 一对多的正向查询
# book_obj = Book.objects.filter(title="三体2").first()
# print(book_obj.publish.name)
# 一对多的反向查询
# 查询对应出版社的书籍
pub_obj = Publish.objects.filter(name='人民出版社').first()
pub_book_name = pub_obj.book_set.all()
# print(pub_book_name)
for name in pub_book_name:
print(name)
 
 
61-基于对象跨表查询之多对多
# 2.多对多的查询的正向查询
# 查询书的作者的名称
# book_obj = Book.objects.filter(title="暗网").first()
# print(book_obj)
# book_obj_author = book_obj.authors.all()
# print(book_obj_author)
# for name in book_obj_author:
# print(name)
# 查找出版过的所有的书籍名称
# author_name = Author.objects.filter(name='panda').first()
# book_list = author_name.book_set.all()
# for book in book_list:
# print(book.title)
 
 
62-基于对象跨表查询之一对一
# 一对一的查询
# 查询一个作者的手机号
# author_num = Author.objects.filter(name="panda").first()
# print(author_num.authordetail.telephone)
# ad = AuthorDeatil.objects.filter(telephone=110).first()
# print(ad.author.name)
 
 
63-基于对象跨表查询的sql语句
64-基于双下划线的跨表查询之一对多1
65-基于双下划线的跨表查询之一对多2
66-基于双下划线的跨表查询之多对多1
67-基于双下划线的跨表查询之多对多2
68-基于双下划线的跨表查询之一对一
# 2.多对多的查询的正向查询
# 查询书的作者的名称
# book_obj = Book.objects.filter(title="暗网").first()
# print(book_obj)
# book_obj_author = book_obj.authors.all()
# print(book_obj_author)
# for name in book_obj_author:
# print(name)
# 查找出版过的所有的书籍名称
# author_name = Author.objects.filter(name='panda').first()
# book_list = author_name.book_set.all()
# for book in book_list:
# print(book.title)
# +-----------------------------------------------------------+
# 一对一的查询
# 查询一个作者的手机号
# author_num = Author.objects.filter(name="panda").first()
# print(author_num.authordetail.telephone)
# ad = AuthorDeatil.objects.filter(telephone=110).first()
# print(ad.author.name)
# +-----------------------------------------------------------+
# 基于双下滑线的跨表查询(join查询)
# 正向查询按字段,反向查询按表名小写用来告诉orm引擎join哪张表
# book_obj = Book.objects.filter(title="暗网").values("publish__name")
# for k in book_obj:
# print(k['publish__name'])
# 方式2
# ret = Publish.objects.filter(book__title="暗网").values("name")
# for i in ret:
# print(i['name'])
# +-----------------------------------------------------------+
# 多对多查询
# 必须告诉引擎通过什么方式来join表中的字段
# 通过Book表join与其关联的Author表
# ret = Book.objects.filter(title="暗网").values("authors__name")
# print(ret)
# 方式二
# ret = Author.objects.filter(book__title="暗网").values("name")
# print(ret)
# +-----------------------------------------------------------+
# 一对一查询
# ret = Author.objects.filter(name='panda').values('authordetail__telephone')
# print(ret)
# ret = AuthorDeatil.objects.filter(author__name="panda").values("telephone")
# print(ret)

posted on 2018-10-09 15:22  pandaboy1123  阅读(156)  评论(0编辑  收藏  举报

导航