Django_ORM数据表查询总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django_ORM数据表查询总结相关的知识,希望对你有一定的参考价值。
class Author(models.Model): ‘‘‘作者表‘‘‘ name = models.CharField(max_length=32) book = models.ManyToManyField(to="Book") # def __str__(self): # return self.name class Book(models.Model): ‘‘‘书表‘‘‘ title = models.CharField(max_length=32) price = models.CharField(max_length=32) publish = models.ForeignKey(to="Publish") # def __str__(self): # return self.title #egon出版过的书的出版社信息 class Publish(models.Model): ‘‘‘出版社‘‘‘ address = models.CharField(max_length=32) # def __str__(self): # return self.address
# author = models.Author.objects.all() #查询到所有的作者对象queryset author多对多book # book = models.Book.objects.all() # 查询到所有的书籍对象queryset book多对一publish # publish = models.Publish.objects.all() #查询到所有的出版社对象queryset # #================================================字段查询============================================================ # for te in author: # print("======author",te.name) #查询自己表内的信息 # print("======author_book",te.book.first().title) #通过自己查找多对多的字段 # print("======author_book_publish",te.book.first().publish.address) #通过自己查找多对多的字段,然后查找多对一字段 # for te in book: # print("========tetetet",te) # print("======book",te.title,te.price) #查询自己表内的信息 # print("========book_publish",te.publish.address) #通过自己查询多对一的出版社表 # print("==========book_author",te.author_set.first()) #通过自己反向查找作者姓名 # for te in publish: # print("=====publish",te.address) #查询自己表内的信息 # print("=====publish_book",te.book_set.first().title) # print("=======publish_book_author",te.book_set.first().author_set.first()) # # # author_book_publish = author.first().book.all().first().title # print("==================================",author_book_publish) # #============================================双下划线正向查询=========================================================== # author = models.Author.objects.all() # 查询到所有的作者对象queryset author多对多book # book = models.Book.objects.all() # 查询到所有的书籍对象queryset book多对一publish # publish = models.Publish.objects.all() # 查询到所有的出版社对象queryset # book_title = author.filter(name="egon").values("book__title") #在author表中查book标的title # book_price = author.filter(name="egon").values("book__price") #在author表中查book表的price # book_publish_address = author.filter(name="egon").values("book__publish__address") #在author表中二次关联查找出版社 # print("============book_title|book_price",book_title,book_price) # print("============book_publish_addredd",book_publish_address) # #============================================双下划线反向查询=========================================================== # book_title = publish.filter(address="北京").values("book__title") # book_price = publish.filter(address="北京").values("book__price") # publish_book = publish.filter(address="北京").values("book__author") # publish_author_book = publish.filter(address="北京").values("book__author__name") # print("============book_title|book_price",book_title,book_price) # print("============publish_book",publish_book) # print("============publish_author_book",publish_author_book) ########################################################################################################################
以上是关于Django_ORM数据表查询总结的主要内容,如果未能解决你的问题,请参考以下文章