django第9天
models类
class Book(Model):
id = AutoField(primary_key=True)
name = CharField(max_length=20)
price = DecimalField(max_digits=5,decimal_places=2)
publish_date = DateField(auto_now_add=True)
publish = ForeignKey(to="Publish",to_field='id')
author = ManyToManyField(to="Author")#多对多关系字段,该字段不会在表中形成字段,是创建关系表的字段,会找到两个表的主键,进行建表
类属性id,name是为了在数据库中建立表字段使用,是代理类(autofield,charfield....)的对象
外键:
publish = Foreignkey(to = 'Publish',to_field='id')
Book类对象的publish属性对应的是Publish表的一个对象,
也就是说通过Book().publish得到的对象包含了关系表的对应记录
orm将publish属性加后缀__id(to_field字段)作为book表的一个字段
使用了外键后,需要先创建好Publish表才能插入Book表数据
多表增删改
一对多关系
#一个出版社对应多本书,
#书依赖于出版社
#增先需要增出版社
#book: name,price,publish_date,publish
#author:name, author_detail
#authordetail:age,telephone,info
#publish:name,addr
1.增
#通过字段
publish = Publish.objects.create(
name = "老男孩出版社",addr = "上海")
Book.objects.create(name = "python",price=66.66,publish_date='1990-1-2',publish_id = publish.id)
publish = Publish.objects.create(name="oldboy",addr="上海")
book = Book.objects.create(name="python",price=66.66,publish_date='1999-01-01',publish_id = publish.id)
#通过属性
p1 = Publish.objects.first()
Book.objects.create(name="go",price=88.88,publish_date = '1970-1-31',publish = p1)
publish = Publish.objects.first()
book = Book.objects.create(name="go",price=55.55,publish_date='1998-01-01',publish = publish)
2.删
#单表删除
book = Book.objects.last().delete()
# book = Book.objects.first().delete()
#book是一,删除时就是删除单条记录
#级联删除
Publish.objects.first().delete()
# Publish.objects.all().delete()
#publish是多,删除时,将关联的book都删除掉
3.改
book的出版社修改必须为已存在的出版社
publish = Publish.objects.create(name='mygirl',addr='北京')
Book.objects.filter(id = 3).update(publish = publish)#属性修改
Book.objects.filter(id = 3).update(publish_id = publish.id)#字段修改
一对一关系
通过外键所在表决定依赖关系
#book: name,price,publish_date,publish
#author:name, author_detail
#authordetail:age,telephone,info
#publish:name,addr
author ---- author_detail
增:
遵循操作顺序
先增author_detail表
author_detail = AuthorDetail.objects.create(age=18,telephone=13588888888,info='真有钱')
Author.objects.create(name = 'zb',author_detail = author_detail)
删
# author_detail = AuthorDetail.objects.create(age=18,telephone=12311112222,info='真的渣')
# Author.objects.create(name='xzh',author_detail = author_detail)
# author_detail = AuthorDetail.objects.create(age = 17,telephone = 32122221111,info='真的2')
# Author.objects.create(name = 'xhz',author_detail_id = author_detail.id)
# Author.objects.first().delete()#删除作者,信息不会被清除,依赖明细表
# AuthorDetail.objects.last().delete()#删除作者明细,作者会被删除,作者明细表被依赖表
改:1对1一般不考虑关联字段
多对多关系
1.多对多关系存在关系表,关系表建议采用manytomanyfield字段处理
book表与author表,多对多
#book: name,price,publish_date,publish
#author:name, author_detail
通过关系表字段存在的类的对象获取关系表
关系表:book.author
增:
为book添加作者的主键或对象们
book.author.add(*args)
#对象
# b1 = Book.objects.first()
# a1 = Author.objects.first()
# b1.author.add(a1)
#主键
# b2 = Book.objects.last()
# a2 = Author.objects.last()
# b2.author.add(a2.id)
'''
添加多条
a1 = Author.objects.first()
b2 = Book.objects.last()
a2 = Author.objects.last()
b2.author.add(a1,a2)
'''
删:删除书籍已有作者的主键或对象们
book.author.remove(*args)
# b2 = Book.objects.last()
# a2 = Author.objects.last()
# b2.author.remove(a2.id)
改:清空并添加作者的主键或对象|设置作者的主键或对象形式的列表
#book对象,不能是列表
#清空book对应作者们的所有对应关系
book.author.clear()
book.author.add(*args)
#注:清空会将id清除,也就说原book对象的id会变更
book.author.set([*args])
#id不变,原作者id,不在列表中的清空,在列表中保留,
a1 = Author.objects.first()
book = Book.objects.last()
book.author.set([a1.id])
# book = Book.objects.last()
# print(book)
# book.author.clear()
跨表查询规则
1.正向逆向概念:
从存放外键的表到关系表叫正向查询反之称为逆向查询
2.正向查询通过外键属性名进行跨表查询
3.逆向查询通过关联表对应类名小写进行跨表查询
基于对象的跨表查询
在跨表查询的规则上,跨表查询的结果为多条数据时需要在字段后添加_set
一对一
author = Author.objects.first()
#正向通过字段名author_detail
author_detail =author.author_detail
#逆向获取作者对象,通过表名小写
author = author_detail.author
'''
author = Author.objects.first()
author_d = author.author_detail
print(author_d.info)
author_d = AuthorDetail.objects.all()[1]
a1 = author_d.author
print(a1.name)
'''
#book: name,price,publish_date,publish
#author:name, author_detail
#authordetail:age,telephone,info
#publish:name,addr
一对多
正向:
book = Book.objects.first()
publish = book.publish
#book = Book.objects.first()
#publish = book.publish
#print(publish.name,publish.addr)
逆向:
publish = Publish.objects.first()
book_list = publish.book_set.all()
#publish = Publish.objects.all()[1]
#book_list = publish.book_set.all()
#print(book_list)
多对多
正向:
book = Book.objects.first()
author_list = book.author.all()
'''
book = Book.objects.first()
print(book.name)
author_list = book.author.all()
'''
逆向:
author = Author.objects.first()
book_list = author.book_set.all()
#author = Author.objects.first()
#print(author.name)
#book_list = author.book_set.all()
#print(book_list[0].name,book_list[1].name)
多级跨表
某作者出版的第一本书的出版社名字
author = Author.objects.filter(name ='xzh').first()
book = author.book_set.first()
book.publish.name
基于下划线的跨表查询
满足跨表查询规则
filter方法与values方法支持__查询规则
# 案例
# 两表关联:查询所有小于18岁作者的名字与实际年龄
'''
authors_dic = Author.objects.filter(author_detail__age__lt=18).values('name', 'author_detail__age')
print(authors_dic)
'''
# 多表关联:查询出版社在上海的出版过的所有书的 作者姓名、作者电话、具体出版社名 的相关信息
info_dic = Book.objects.filter(publish__address__contains="上海").values('author__name', 'author__author_detail__telephone', 'publish__name')