我有一個產品模型,帶有一個外鍵給一些價格,我真的想列出產品的「最佳」報價...如何做那?Django濾波器模型一對多關係,價格差異最大
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)
首先我想所有的產品具有一個以上的代價,那我:
ps = Product.objects.annotate(c=Count("price")).filter(c__gt=2)
現在我想最好的6種產品,這兩個最新的價格之間的差異最大。
任何人都可以幫忙嗎?我希望這是有道理的;)
請注意,這不能與sqlite一起使用...更多信息,請訪問https://docs.djangoproject.com/en/1.6/ref/models/querysets/#stddev – juliocesar
你知道如何獲得產品嗎?最新的價格是最低的? ;) – pkdkk