models.Book.objects.filter(**kwargs): querySet [obj1,obj2]
models.Book.objects.filter(**kwargs).values(*args) : querySet [{},{},{}]
models.Book.objects.filter(**kwargs).values_list(title) : querySet [(),(),()]
Cross-table query summary:
1, create a table
class Book (models.Model): title = models.CharField (MAX_LENGTH = 32 ) publish = models.ForeignKey ( " the Publish " ) # Create many foreign key field AuthorList = models.ManyToManyField ( " the Author " ) # plurality of many relationships, automatically creates table class the Publish (models.Model): name = models.CharField (MAX_LENGTH = 32 ) addr = models.CharField (MAX_LENGTH = 32 ) class the Author (models.Model): name = models.CharField ( = 32 MAX_LENGTH ) Age = models.IntegerField () AD = models.models.OneToOneField ( " AuthorDetail " ) # create a one to one relationship class AuthorDetail (models.Model): tel = models.IntegerField ()
2, based on the object relational query:
IF-to-many query (Book - Publish):
forward query by field:
book_obj.publish: Press book_obj.publish.addr objects associated with this book: This book is associated with publishers address
reverse lookup, by table name _set
publish_obj.book_set: press associated with this book collection of objects publish_obj.book_set.all (): [obj1, obj2, ....]
IF one query (author --- AuthorDetail):
forward query by field:
author_obj.ad: author is associated with this author details Object
reverse lookup: table name:
author_detail_obj.author: detailed objects associated with the author of the object
if to-many (Book ---- Author):
Forward query by field:
book_obj.authorList.all (): This book is a collection associated with all of the objects [obj1, obj2, ....]
book_obj.authorList.all () values ( "name" ):. If you want to check a single value so check when you can
reverse lookup, by table name _set:
author_obj.book_set.all (): The author is associated with all the books collection of objects
. Book_obj.book_set.all () values ( "name"): If you want to check a single value when you can check this
Based on the decline across the table double line of inquiry:
if many queries (Book - Publish):
Forward inquiry, according to field:
# Inquiry linux book publishers name: . Models.Book.objects.all () filter (title = " linux " ) .values ( " publish__name " )
Reverse Lookup: Table name:
# Inquiry People's Publishing House published the names of all the books models.Publish.objects.filter (name = " People's Publishing House " ) .values ( " book__title " )
if one query (Author --- AuthorDetail):
Forward inquiry, according to field:
# Query egon phone number models.Author.objects.filter (name = " egon " ) .values ( " ad__tel " )
Reverse Lookup: Table name:
# Inquiry phone number is the author of 151 models.AuthorDetail.objects.filter (tel = " 151 " ) .values ( " author__name " )
if-many (Book ---- Author):
Forward inquiry, according to field:
#查询python这本书的作者的名字 models.Book.objects.filter(title="python").values("authorList__name") [{},{},{},{}]
正向查询,按表名:
#查询alex出版过的出的价格 models.Author.objects.filter(name="alex").values("book__price")
注意:
publish=models.ForeignKey("Publish",related_name="bookList")
authorlist=models.ManyToManyField("Author",related_name="bookList")
ad=models.models.OneToOneField("AuthorDetail",related_name="authorInfo")
反向查询的时候都用:related_name的值
聚合查询:
querySet().aggregate(聚合函数)------返回的是一个字典,不再是一个querySet
Book.objects.all().aggregate(average_price=Avg('price'))
分组查询:
querySet().annotate() --- 返回的是querySet
#统计每一个出版社中最便宜的书籍的价格
sql: select Min(price) from book group by publish_id; ORM: models.Book.objects.values("publish__name").annotate(Min("price"))
models.Book.objects.filter(**kwargs): querySet [obj1,obj2]
models.Book.objects.filter(**kwargs).values(*args) : querySet [{},{},{}]
models.Book.objects.filter(**kwargs).values_list(title) : querySet [(),(),()]
跨表查询总结:
1、创建表
class Book(models.Model): title = models.CharField(max_length=32) publish=models.ForeignKey("Publish") # 创建一对多的外键字段 authorList=models.ManyToManyField("Author") # 多对多的关系,自动创建关系表 class Publish(models.Model): name = models.CharField(max_length=32) addr = models.CharField(max_length=32) class Author(models.Model): name=models.CharField(max_length=32) age=models.IntegerField() ad=models.models.OneToOneField("AuthorDetail") #创建一对一的关系 class AuthorDetail(models.Model): tel=models.IntegerField()
2、基于对象关联查询:
if 一对多查询(Book--Publish):
正向查询,按字段:
book_obj.publish : 与这本书关联的出版社对象 book_obj.publish.addr: 与这本书关联的出版社的地址
反向查询,按表名_set
publish_obj.book_set: 与这个出版社关联的书籍对象集合 publish_obj.book_set.all() :[obj1,obj2,....]
if 一对一查询(Author---AuthorDetail):
正向查询,按字段:
author_obj.ad : 与这个作者关联的作者详细信息对象
反向查询:按表名:
author_detail_obj.author : 与这个作者详细对象关联的作者对象
if 多对多(Book----Author):
正向查询,按字段:
book_obj.authorList.all(): 与这本书关联的所有这作者对象的集合 [obj1,obj2,....]
book_obj.authorList.all().values("name"): 如果想查单个值的时候可以这样查
反向查询,按表名_set:
author_obj.book_set.all() : 与这个作者关联的所有书籍对象的集合
book_obj.book_set.all().values("name"): 如果想查单个值的时候可以这样查
基于双下滑线的跨表查询:
if 一对多查询(Book--Publish):
正向查询,按字段:
# 查询linux这本书的出版社的名字: models.Book.objects.all().filter(title="linux").values("publish__name")
反向查询:按表名:
# 查询人民出版社出版过的所有书籍的名字 models.Publish.objects.filter(name="人民出版社出版").values("book__title")
if 一对一查询(Author---AuthorDetail):
正向查询,按字段:
#查询egon的手机号 models.Author.objects.filter(name="egon").values("ad__tel")
反向查询:按表名:
#查询手机号是151的作者 models.AuthorDetail.objects.filter(tel="151").values("author__name")
if 多对多(Book----Author):
正向查询,按字段:
#查询python这本书的作者的名字 models.Book.objects.filter(title="python").values("authorList__name") [{},{},{},{}]
正向查询,按表名:
#查询alex出版过的出的价格 models.Author.objects.filter(name="alex").values("book__price")
注意:
publish=models.ForeignKey("Publish",related_name="bookList")
authorlist=models.ManyToManyField("Author",related_name="bookList")
ad=models.models.OneToOneField("AuthorDetail",related_name="authorInfo")
反向查询的时候都用:related_name的值
聚合查询:
querySet().aggregate(聚合函数)------返回的是一个字典,不再是一个querySet
Book.objects.all().aggregate(average_price=Avg('price'))
分组查询:
querySet().annotate() --- 返回的是querySet
#统计每一个出版社中最便宜的书籍的价格
sql: select Min(price) from book group by publish_id; ORM: models.Book.objects.values("publish__name").annotate(Min("price"))