1. 程式人生 > >Django_ORM數據表查詢總結

Django_ORM數據表查詢總結

django orm model 查詢api

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數據表查詢總結