I have a model of a product with a foreign key at some prices, and I really want to list products with the "best" offer ... How to do this?
class Product(models.Model): productname = models.CharField(max_length=1024) class Price(models.Model): product = models.ForeignKey(Product) price = models.DecimalField(max_digits=10, decimal_places=2) created = models.DateTimeField(auto_now_add=True)
First, I want all products with more than one price that I received:
ps = Product.objects.annotate(c=Count("price")).filter(c__gt=2)
Now I want the best 6 products with the biggest difference between the last two prices.
Can anyone help with this? Hope this makes sense;)
source share