1, the forward and reverse lookup queries concept
# Forward queries by field, by reverse lookup table name in lowercase # One # Forward: author --- author related fields in the table ---> authordetail by field # Reverse: authordetail --- author related fields in the table ---> author by table name in lowercase # query jason phone number of positive inquiry # inquiry address is: name of the author of Shandong reverse lookup # Many # Forward: book --- book table in the associated field ---> publish press field # Reverse: publish --- related field book list ---> book by table name in lowercase _set .all () as a corresponding plurality Press Books # -Many # Forward: book --- book table in the associated field ---> author by field # reverse: author --- related field book list ---> book by table name in lowercase _set .all () because of a corresponding multiple books
2, object-based query table
# Forward # query books Three Kingdoms publishers mailbox book_obj = models.Book.objects.filter (title = ' Three Kingdoms ' ) .first () Print (book_obj.publish.email) # query is the author of books of the Three Kingdoms name book_obj = models.Book.objects.filter (title = 'Three Kingdoms ' ) .first () Print (book_obj.authors) # app01.Author.None Print (book_obj.authors.all ()) # query author jason telephone number USER_OBJ = models.Author.objects.filter (name = ' Jason ' ) .first () Print (user_obj.authordetail.phone) # Reverse # query Oriental Press Press is publishing books-many fields reverse lookup publish_obj = models.Publish.objects.filter (name = ' Oriental Press ' ) .first () Print (publish_obj.book_set) # app01.Book.None Print (publish_obj.book_set.all ()) # Reverse lookup all the books-many fields of inquiry jason wrote author_obj = models.Author.objects.filter (name = ' jason ' ) .first () Print (author_obj.book_set) # app01.Book.None Print (author_obj.book_set.all ()) # Inquiry telephone number of the author's name is one of 110 fields reverse lookup authordetail_obj = models.AuthorDetail.objects.filter (Phone = 110 ) .first () Print (authordetail_obj.author.name)
3, based on two lines of inquiry decline
# Forward # query books for the publishing house of the Three Kingdoms address; publish__addr publish the list goes on, addr is the field of the table RES = models.Book.objects.filter (title = ' Three Kingdoms ' ) .values ( ' publish__addr ' , ' title ' ) Print (RES) # query books for the author of the Three Kingdoms names RES = models.Book.objects.filter (title = ' Three Kingdoms ' ) .values ( " authors__name " , ' title ' ) Print ( RES) # query author jason hometown RES = models.Author.objects.filter (name = ' jason ').values('authordetail__addr') print(res) # Reverse # query Southern Publishing House title RES = models.Publish.objects.filter (name = ' Southern Press ' ) .values ( ' book__title ' ) Print (RES) # inquiry telephone number for the name of the author of 120 = models.AuthorDetail.objects.filter RES (Phone = 120) .values ( ' author__name ' ) Print (RES) # query to the name of the book written by jason of RES = models.Author.objects.filter (name = ' jason ' ) .values ( ' book__title ' ) Print (RES) # query books for the author of the Three Kingdoms phone number res = models.Book.objects.filter(title='三国演义').values('authors__authordetail__phone') print(res)
4, forward, reverse mixed case
# Query jason author's phone number # Forward RES = models.Author.objects.filter (name = ' jason ' ) .values ( ' authordetail__phone ' ) Print (RES) # reverse (table name in lowercase) RES = models.AuthorDetail .objects.filter (author__name = ' Jason ' ) .values ( ' Phone ' ) Print (RES) # All books queries Oriental Press Press for the names and prices # reverse RES = models.Publish.objects.filter (name = ' Oriental Press ' ) .values ( ' book__title ' , ' book__price ' ) Print (RES ) # forward RES = models.Book.objects.filter (publish__name = ' Oriental Press ' ) .values ( ' title ' , ' . price ' ) Print (RES) # Query Oriental Press published price is greater than 400 books # Forward RES = models.Publish.objects.filter (name = " Oriental Press " , book__price__gt = 400) .values ( ' book__title ' , ' book__price ' ) Print ( RES) # reverse RES = models.Book.objects.filter (price__gt = 400, publish__name = ' Oriental Press ' ) .values ( ' title ' , ' . price ' ) Print (RES)
5, the polymerization, the query packet
View profile configuration parameters sql statement orm all internal operations:
# The following can be written in settings.py, the output result will show corresponding SQL statement the LOGGING = { ' Version ' :. 1 , ' disable_existing_loggers ' : False, ' handlers ' : { ' Console ' : { ' Level ' : ' the DEBUG ' , ' class ' : ' logging.StreamHandler ' , }, }, 'loggers': { 'django.db.backends': { 'handlers': ['console'], 'propagate': True, 'level':'DEBUG', }, } }
# Need to pilot test file package from django.db.models Import Max, Min, the Count, Sum, Avg # Aggregate aggregate queries # query the number of all the books, count_num is an alias RES = models.Book.objects.filter (PK = 3) .aggregate (count_num the Count = ( ' in the authors ' )) Print (RES) # query all publishing the average price agency published a book of RES = models.Publish.objects.aggregate (avg_price = Avg ( ' book__price ' )) Print (RES) # 4498.636 # count the number of books published by the East res = models.Publish.objects .filter (name = ' Oriental Press ' ) .aggregate (count_num the Count = ( ' book__id ' )) Print (RES) # Grouped query (group_by) Annotate # statistics for each book published by the average price of RES = models.Publish.objects.annotate (avg_price = Avg ( ' book__price ' )). Values ( ' name ' , ' avg_price ' ) Print (RES) # statistics of the number of each book RES = models.Book.objects.annotate (count_num the count = ( ' in the authors ' )). values ( ' title ' , ' count_num ' ) Print (RES) # statistics each publisher to sell the cheapest book prices res = models.Publish.objects.annotate (min_price = Min (' Book__price ' )). Values ( ' name ' , ' MIN_PRICE ' ) Print (RES) # query of the total price of each book RES = models.Author.objects.annotate (sum_price = Sum ( ' book__price ' )) .values ( ' name ' , ' sum_price ' ) Print (RES)