Django_ aggregation and grouping query

Aggregate query: aggregate (), the return value is a dictionary.

# Import feature functions 
from django.db.models Import Avg, Max, Min, the Count
 # query all the books of the average price, maximum price, minimum price, the total price, you can customize the dictionary keys: aggregate (wdc = Avg ( " price ")) 
ret = models.Book.objects.all (). aggregate (Avg ( " price " ), Max ( " price " ), Min ( " price " ), Count ( " price " ))
 print (ret)

result:

 

 Group query: annotate (), the return value is an object.

from django.db.models Import Avg, Max, Min, the Count
 # inquiry average salary values ( "to be grouped field") in each department .annotate (field to statistics) 
RET = models.Wdc.objects.values ( " dep " ) .annotate (Avg ( " salary " ))
 print (ret)

result:

 

Guess you like

Origin www.cnblogs.com/wangdianchao/p/12741846.html