In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "aggregate function, grouping function and the usage of F, Q query in django". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The aggregation is aggreate (* args,**kwargs), which is calculated through QuerySet. When doing evaluation operations, use the
Grouping is annotate (* args,**kwargs). There are conditions in parentheses. When you encounter something, you have to group.
First import the functions to be used from models
From django.db.models import Min,Avg,Max Sumdef aggregate (request): # find the average price of all books ret11 = Book.objects.all (). Aggregate (Avg ("price")) # print (ret11) # {'price__avg': 51.975} # the highest price of all books ret12 = Book.objects.all (). Aggregate (MaxPrice=Max (' price')) # print (ret12) # {'MaxPrice': Decimal (' 100.00')} # egon the highest price ever published Price p = Book.objects.filter (authors__name='egon'). Price (Max ('price')) print (p) # {' price__max': Decimal ('100.00')} # Group annotate () # the highest price of a book ever published by each author # parsing, grouping by author's name, will use values, which shows the name of the field, b1 = Book.objects.values ('authors__name') .annotate (Max (' price')) print (b1) # # four authors are all associated with the same book And this book is the highest price # the lowest price book published by each publishing house b2 = Book.objects.values ('publish__name') .annotate (Min (' price')) print (b2) #
F Q query
F, mainly to make queries.
Q query can be used to query a combination of conditions & yes and, | (pipe character) or, ~ not
Def FAndQ (request): # increase the price of each book by 10 yuan, # write update book set price=price+10 # F with SQL sentence, mainly to do the query # Book.objects.all (). Update (price=F ('price') + 10) # query books with a rich beginning and a price greater than 30 Comma can do with the relationship query b4 = Book.objects.filter (title__startswith= "rich", price__gt=30) # print (b4) # # Q query can do a combination of conditions of the query & is with, | (pipe character) or, ~ non # # query books are rich at the beginning Or books with a price greater than 30, b5 = Book.objects.filter (Q (title__startswith= "rich") | Q (price__gt=60)) # print (b5) # # combined query, starting with rich, with a price greater than 60, or books with id greater than 5 b6 = Book.objects.filter (Q (title__startswith= "rich") & Q (price__gt=60) | Q (id__gt=5)) # print (b6) # query conditions if Q is not added You have to put it at the back. B7 = Book.objects.filter (Q (price__gt=60) | Q (id__gt=5), title__startswith= "rich") # print (b7) # ~ negative meaning b8 = Book.objects.filter (Q (title__startswith= "rich") & Q (price__gt=60) | Q (id__gt=5)) print (b8) "aggregate function, grouping function and introduction to F and Q query in django". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.