On Jun 4, 10:20 pm, "P. Kaminski" <cge...@gmail.com> wrote:
> Hello,
> I'm studying the Django page on aggregation
> In the section 'Joins and aggregates' there's an example of how to
> create an annotation for each Store with book price ranges. But what
> if I want to do the opposite, i.e. for each available Book, look up
> through the stores and create a price range from this.
The Book model on that page has a price field. Because of that the
book price is constant across stores and your range would always be
just that number. So first you probably want to change the model to
something like this:

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'))


> This backward-looking procedure is what's I'm most interested in.
> Best regards,
> Przemek

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to