英文:
Django ORM - How to get max price and the associated date
问题
以下是您要翻译的内容:
"关注我在杂货店购买的物品价格。
我有以下这些模型:
Class Product(models.Model):
title = models.CharField(max_length=255)
name = models.CharField(max_length=255)
information = models.CharField(max_length=255)
Class Price(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
date_seen = models.DateTimeField(auto_now_add=True)
price = models.IntegerField(blank=True, null=True)
我尝试为每个产品获取最高价格和相关日期。
如果我执行以下操作:
def get_max_price_for_product(request):
max_price_list = (
Price.objects.values("product__title", "product__name")
.annotate(score=Max("price"))
.order_by("-price")
)
context = {"max_price_list": max_price_list}
return render(request, "prices/max_list.html", context)
我可以获得每个产品的最高价格,但没有日期信息。我阅读了Django文档,但没有找到(或者我没有理解我所阅读的内容)如何做到这一点。
有人可以帮助我吗?
提前谢谢。"
英文:
To follow the price of what I buy at the grocery.
I have these models :
Class Product(models.Model):
title = models.CharField(max_length=255)
name = models.CharField(max_length=255)
information = models.CharField(max_length=255)
Class Price(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
date_seen = models.DateTimeField(auto_now_add=True)
price = models.IntegerField(blank=True, null=True)
And I try to have for each product the max price and the associated date.
If I do
def get_max_price_for_product(request):
max_price_list = (
Price.objects.values("product__title", "product__name")
.annotate(score=Max("price"))
.order_by("-price")
)
context = {"max_price_list": max_price_list}
return render(request, "prices/max_list.html", context)
I've the max price for each product but without the date. I read the Django doc but I didn't find (or I didn't understand what I read) how to do this.
Can somebody help me ?
Thank you in advance.
答案1
得分: 1
以下是翻译好的部分:
可能更好的做法是使用`QuerySet`,以`Product`作为主要模型,如下所示:
```python
from django.db.models import Max
Product.objects.annotate(max_price=Max('price__price'))
这样,您将检索具有额外属性.max_price
的Product
对象,但它仍然像Product
对象一样运行,因此您可以跟踪ForeignKey
等。
您可以通过使用子查询来获取修改日期:
from django.db.models import F, Max, OuterRef, Subquery
def get_max_price_for_product(request):
Product.objects.annotate(
max_price=Max('price__price'),
max_price_date=Subquery(
Price.objects.filter(product_id=OuterRef('pk'))
.order_by(F('price').desc(nulls_last=True))
.values('date_seen')[:1]
),
)
context = {'max_price_list': max_price_list}
return render(request, 'prices/max_list.html', context)
这将检索最大价格的date_seen
。
请注意,`<b>`和`</b>`不是翻译部分,它们是HTML标签,用于加粗文本。
<details>
<summary>英文:</summary>
It might be better to work with a `QuerySet` with `Product` as the main model, so:
<pre><code>from django.db.models import Max
Product.objects.annotate(<b>max_price=Max('price__price')</b>)</code></pre>
that way you retrieve `Product` objects with an extra attribute `.max_price`, but it still functions like a `Product` object, so you can follow `ForeignKey`s, etc.
You can obtain the modification date by using a subquery:
<pre><code>from django.db.models import F, Max, OuterRef, Subquery
def get_max_price_for_product(request):
Product.objects.annotate(
max_price=Max('price__price'),
max_price_date=<b>Subquery(</b>
Price.objects.filter(product_id=OuterRef('pk'))
.order_by(F('price').desc(nulls_last=True))
.values('date_seen')[:1]
<b>)</b>,
)
context = {'max_price_list': max_price_list}
return render(request, 'prices/max_list.html', context)</code></pre>
This will retrieve the `date_seen` for the largest price.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论