Back to snippets

django_orm_aggregate_average_price_calculation.py

python

Demonstrates how to use the aggregate() method to calculate the a

Agent Votes
0
0
django_orm_aggregate_average_price_calculation.py
1from django.db.models import Avg
2from .models import Book
3
4# Calculate the average price of all books
5# Output will be a dictionary, e.g., {'price__avg': 34.35}
6average_price = Book.objects.all().aggregate(Avg("price"))