单表操作
1.简单增删改查
User.objects.create() | update() | delete()
user = User() user.save() | delete()
2.查询的操作函数
all | filter | first | last | values
3.模糊查询
id__gt = 10 name__contains='a' (%a%) birthday_year = 2018
4.F与Q查询
基于计算 F('tag_name')
基于关系Q(条件) ==> Q(条件一) & Q(条件二)
Q(条件一) | Q(条件二) ~Q(条件1)
设计Models
一对多:外键在多的一方
一对一:外键在任意一方均可
多对多:建立关系表
一对多关系:Book 依赖于Publish
1.增
主键操作
publish = Publish.objects.create(name='sa',address='shandong')
publish_id = publish.id
Book.objects.create(name='死亡Python', price=66.66, publish_date='2018-8-8', publish_id=publish_id)
对象操作
publish = Publish.objects.first()
Book.objects.create(name='*mei',price=12.09,publish_date='1980-1-4',publish = publish)
2.删
book = Book.objects.last()
book.delete()
Publish.objects.first().delete()
3.改
publish = Publish.onjects.last()
book = Book.objects.last()
book.publish = publish
book.save()
publish_id = Publish.objects.last().id
Book.onjects.filter(name='siwang').update(publish_id = publish_id)
Author:id name
AuthorDetail:id age telephone info
二,一对一关系
1.增
detail = AuthorDetail.on=bjects.create(age = 8,telephone=12375823,info='dwq')
Author.objects.create(name='wo',author_detail=detail)
Author,objects.create(name='fr',author_datail_id=detail_id)
detail = AuthorDetail.objects.create(age=66,telephone=1234412,ibfo='fvre')
Author.objects.create(name='de',author_detail_id = detail.id)
2.删
AuthorDetail.objects.last().delete()
3.一对一关系不考虑连表更新
三多对多
关联关系存在于关系表
如果获取关系表,通过Book的对象获得author属性,代表关系表的对象
book = Book.objects.first()
book.author代表关系表的对象
detail = AuthorDetail.objects.create(age='44',telephone=12314,info='grteg')
Author.objects.create(name='qwe',author_detail_id=detail.id)
b1 = Book.objects.first()
b2 = Book.objects.all()[1]
a1 = Author.objects.first()
a2 = Author.objects.all()[1]
1.增add()
book.author.add()
b1.author.add()a1.id,a2.id)
b2.author.add(a1,a2)
2.删
将第二本书的关系记录全部清除
b2.author.clear()
通过主键 或对象操作指定某条记录
b1.author.remove(a1)
b1.author.remove(a2.id)
3.改
清除该本书之前的所有关系记录添加新的关系
b1.author.clear()
b1.author.add(a1)
取出新数据中不存在的值,添加新数据中新有的值,与之前重复的值保留
b2.author.set([a2.id])
a3 = Author.objects.last()
b2.author.set([a2.id,a3.id]) 更改即为将值设置为列表中的数据
连表查询规则
1.正向逆向概念:从存放外键的表到关系表称之为正向跨表查询,反之称之为逆向查询
2.正向查询通过外键属性名进行跨表查询
3.逆向查询通过关联表对应类名小写进行跨表查询
四,基于对象的多表查询
在连表查询规则之上,逆向查询遇到多条结果,在类名后再添加_set
1.一对多表查询
需求:打印第一本书的出版社名
book = Book.objects.first()
print(book.publish.name)
需求:第一个出版社出版过的书门的名字
publish = Publish.objects.first()
print(publish.book_set.all())
for book in publish.book_set.all():
pritn(book.name)
需求:打印第二本书的出版社地址
address = Book.objects.all()[1].publish.address
print(address)
需求:第二个出版社出版过的书门的价格
publish = Publish.onjects.all()[1]
for book in publish.book_set.all():
print(book=ok,price)
2.一对一:
author = Author.objects.first()
print(author.author_detail.telephone)
author_detail = AuthorDetail.objects.last()
print(author_detail.author.name)
3.多对多
需求:第一本书的作者们的姓名
book = Book.objects.filter(name='jin').first()
print(book.author)
for author in book.author.filter(name__endswith='xx'):
print(author.name)
4.需求:第二个作者写过的书门的书名
author = Author.objects.all()[1]
for book in author,book_set.all():
print(book.name)
需求:第一个出版社出版过的书门的名字与价格
publish = Publish.objects.first()
books = publish.book_set.all()
数据所有字段都被保留存放在对象们中
for book in books:
print(book.name,book.price)
book_infos = publish.book_set.values('name,'price')
print(books_infos)
5.多级跨表查询
需求:第一个出版社出版过书门的作者们的电话号码
publish = Publish.objects.first()
books=publish.book_set.all()
for book in books:
authors = book.author.all()
for author in authors:
print(author.author_detail.telephone)
五,基于双下划线的跨表查询:注不能出现对象
1.一对多
需求:第一本书的出版社名
publish_name = Book.objects.filter(id=5).values('publish__name')
print(publish_name)
需求:第一个出版社出版过的书门
books_infos = Publish.onjects.filter(id=2).values('book__name','book_price)
print(books_infors)
2.一对一
需求:查询所有大于80岁作者的名字与实际年龄
authors_infos = Author.objects.filter(author_detail__age__gt=80).values('name','author_detail__age')
print(authors_infos)
多级连表查询
多表关联:查询出版社在上海的出版过的所有书的作者姓名,作者电话,具体的出版社名,的相关信息
infos = Publish.objects.filter(address_contains='shanghai').values(book__author__name','book__author__author_detail__telephone','name')
print(infos)