ORM in Django polymerization index

ORM in Django polymerization index

 

In Django, polymeric aggregate function is achieved by a method, the method returns aggregate result is a Dictionary

When using the module needs to be imported from django.db.models import Count, Avg, Max, Min, Sum

Suppose you have a model

Copy the code
class Book(models.Model):
    name = models.CharField(max_length=100)
    pages = models.IntegerField()
    price = models.FloatField()
    author = models.CharField(max_length=100)
    class Meta:
        db_table='book'
Copy the code

 

Avg: Averaging

avgprice=Book.objects.aggregate(Avg('price'))

print (avgprice) Print results { "price__avg": 23.0}

The default alias structure field__ method lowercase, if you want to customize the style returns the result, the result can be assigned to a key parameters, such as

avgprice = Book.objects.aggregate (my_avg = Avg ( 'price')), the print result is {my_avg ": 23.0}

The system can be viewed by a print (avgprice.queries) into native sql statement (first imported from django.db import connection)

 

count: Gets the count, do not re-default

bookcount = Book.objects.aggregate(book_num=Count('id'))

In the count, if you want to filter duplicate values, parameters may be added DISTINCT, i.e. book_num = Count (book_num = 'name', distinct = True)

 

Max and Min: maximum and minimum values

max_price=Book.objects.aggregate(max_price=Max('price'))

min_price=Book.objects.aggregate(min_price=Min('price'))

 

Sum: sum

sum=Book.objects.aggregate(sum_price=Sum('price'))

In Django, polymeric aggregate function is achieved by a method, the method returns aggregate result is a Dictionary

When using the module needs to be imported from django.db.models import Count, Avg, Max, Min, Sum

Suppose you have a model

Copy the code
class Book(models.Model):
    name = models.CharField(max_length=100)
    pages = models.IntegerField()
    price = models.FloatField()
    author = models.CharField(max_length=100)
    class Meta:
        db_table='book'
Copy the code

 

Avg: Averaging

avgprice=Book.objects.aggregate(Avg('price'))

print (avgprice) Print results { "price__avg": 23.0}

The default alias structure field__ method lowercase, if you want to customize the style returns the result, the result can be assigned to a key parameters, such as

avgprice = Book.objects.aggregate (my_avg = Avg ( 'price')), the print result is {my_avg ": 23.0}

The system can be viewed by a print (avgprice.queries) into native sql statement (first imported from django.db import connection)

 

count: Gets the count, do not re-default

bookcount = Book.objects.aggregate(book_num=Count('id'))

In the count, if you want to filter duplicate values, parameters may be added DISTINCT, i.e. book_num = Count (book_num = 'name', distinct = True)

 

Max and Min: maximum and minimum values

max_price=Book.objects.aggregate(max_price=Max('price'))

min_price=Book.objects.aggregate(min_price=Min('price'))

 

Sum: sum

sum=Book.objects.aggregate(sum_price=Sum('price'))

Guess you like

Origin www.cnblogs.com/ls011218/p/11817169.html