擅长:python、mysql、java
<p>弄清楚了,我就得到了我想要的。如果有人读到这篇文章,我很乐意得到反馈,如果这是最好的做法,或者我将超载我的数据库。在</p>
<p>因为我需要实际价格和最高价格的日期,所以我需要返回整个价格对象。所以我通过重写默认的<code>get_context_data</code>方法在DetailView上编写了一些查询集。在</p>
<p>在视图.py在</p>
<pre><code>class ProductDetailView(DetailView):
model = Product
def get_context_data(self, **kwargs):
context = super(ProductDetailView, self).get_context_data(**kwargs)
context['high'] = Price.objects.filter(product_id=self.get_object()).order_by('price').last()
context['low'] = Price.objects.filter(product_id=self.get_object()).order_by('-price').last()
context['avg'] = Price.objects.filter(product_id=self.get_object()).aggregate(avg_price=Avg('price'))
</code></pre>
<p>然后我使用<code>high.price</code>和<code>high.date_seen</code>等将其拉入我的模板中</p>