Custom Search

Monday, June 7, 2010

How to aggregate functions in django


aggregation
-----------
class Book(models.Model):
name = models.CharField(max_length=300)

class Store(models.Model):
name = models.CharField(max_length=300)
books = models.ManyToManyField(Book, through='BookPriceInStore',
related_name="stores")

class BookPriceInStore(models.Model)
store = models.ForeignKey(Store, related_name="book_prices")
book = models.ForeignKey(Book, related_name="store_prices")
price = models.DecimalField(max_digits=10, decimal_places=2)

# Then you can say:

Book.objects.annotate(max_price=Max('store_prices__price',
min_price=Min('store_prices__price'))

No comments:

Post a Comment