Tutorial by Examples

class Product(models.Model): name = models.CharField(max_length=20) price = models.FloatField() To Get average price of all products: >>> from django.db.models import Avg, Max, Min, Sum >>> Product.objects.all().aggregate(Avg('price')) # {'price__avg': 124.0} To ...
class Category(models.Model): name = models.CharField(max_length=20) class Product(models.Model): name = models.CharField(max_length=64) category = models.ForeignKey(Category, on_delete=models.PROTECT) To get the number products for each category: >>> categories = Ca...
We can perform a GROUP BY ... COUNT or a GROUP BY ... SUM SQL equivalent queries on Django ORM, with the use of annotate(), values(), order_by() and the django.db.models's Count and Sum methods respectfully: Let our model be: class Books(models.Model): title = models.CharField() ...

Page 1 of 1