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
Book class (models.Model): title = models.CharField (MAX_LENGTH = 32) publish = models.ForeignKey ( "the Publish", on_delete = models.CASCADE) # create many foreign key field authorList = models.ManyToManyField ( " author ") #-many relationship, relational tables are automatically created class Publish (models.Model): name = models.CharField (max_length = 32) addr = models.CharField (max_length = 32) class author (models.Model): = models.CharField name (max_length = 32) 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 query author of this book's name models.Book.objects.filter (title = "python") . Values ( "authorList__name") [{}, {}, {}, {}]
Forward queries, according to the table name:
# Inquiry alex published in the price models.Author.objects.filter (name = "alex") . Values ( "book__price")
note:
= models.ForeignKey publish ( "the Publish", the related_name = "bookList")
AuthorList = models.ManyToManyField ( "the Author", the related_name = "bookList")
AD = models.models.OneToOneField ( "AuthorDetail", the related_name = "authorInfo")
when reverse query are used: related_name value of
aggregate queries:
. QuerySet () Aggregate (aggregate function) ------ returns a dictionary, is no longer a QuerySet
Book.objects.all () Aggregate (. average_price = Avg ( 'price') )
grouping query:
. QuerySet () Annotate () --- returns a QuerySet
# statistics every publishing house in the cheapest book prices
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 [(),(),()]
Cross-table query summary:
1, create a table
Book class (models.Model): title = models.CharField (MAX_LENGTH = 32) publish = models.ForeignKey ( "the Publish", on_delete = models.CASCADE) # create many foreign key field authorList = models.ManyToManyField ( " author ") #-many relationship, relational tables are automatically created class Publish (models.Model): name = models.CharField (max_length = 32) addr = models.CharField (max_length = 32) class author (models.Model): = models.CharField name (max_length = 32) 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 query author of this book's name models.Book.objects.filter (title = "python") . Values ( "authorList__name") [{}, {}, {}, {}]
Forward queries, according to the table name:
# Inquiry alex published in the price models.Author.objects.filter (name = "alex") . Values ( "book__price")
note:
= models.ForeignKey publish ( "the Publish", the related_name = "bookList")
AuthorList = models.ManyToManyField ( "the Author", the related_name = "bookList")
AD = models.models.OneToOneField ( "AuthorDetail", the related_name = "authorInfo")
when reverse query are used: related_name value of
aggregate queries:
. QuerySet () Aggregate (aggregate function) ------ returns a dictionary, is no longer a QuerySet
Book.objects.all () Aggregate (. average_price = Avg ( 'price') )
grouping query:
. QuerySet () Annotate () --- returns a QuerySet
# statistics every publishing house in the cheapest book prices
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 [(),(),()]
Cross-table query summary:
1, create a table
Book class (models.Model): title = models.CharField (MAX_LENGTH = 32) publish = models.ForeignKey ( "the Publish", on_delete = models.CASCADE) # create many foreign key field authorList = models.ManyToManyField ( " author ") #-many relationship, relational tables are automatically created class Publish (models.Model): name = models.CharField (max_length = 32) addr = models.CharField (max_length = 32) class author (models.Model): = models.CharField name (max_length = 32) 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 query author of this book's name models.Book.objects.filter (title = "python") . Values ( "authorList__name") [{}, {}, {}, {}]
Forward queries, according to the table name:
# Inquiry alex published in the price models.Author.objects.filter (name = "alex") . Values ( "book__price")
note:
= models.ForeignKey publish ( "the Publish", the related_name = "bookList")
AuthorList = models.ManyToManyField ( "the Author", the related_name = "bookList")
AD = models.models.OneToOneField ( "AuthorDetail", the related_name = "authorInfo")
when reverse query are used: related_name value of
aggregate queries:
. QuerySet () Aggregate (aggregate function) ------ returns a dictionary, is no longer a QuerySet
Book.objects.all () Aggregate (. average_price = Avg ( 'price') )
grouping query:
. QuerySet () Annotate () --- returns a QuerySet
# statistics every publishing house in the cheapest book prices
sql: select Min(price) from book group by publish_id; ORM: models.Book.objects.values("publish__name").annotate(Min("price"))