我正在构建一个价格比较django应用程序,我遇到了这个场景,我需要在相关字段查找中过滤每个卖家的最后价格。
卖方模型:
class Seller(models.Model):
name = models.CharField(max_length=250, null=True)
部件模型:
class Part(models.Model):
name = models.CharField(null=True, blank=True, max_length=250, unique=True)
卖方模型:
class Price(models.Model):
seller = models.ForeignKey(Seller, on_delete=models.CASCADE, null=True, blank=True, related_name='sellerprice')
part = models.ForeignKey(Part, on_delete=models.CASCADE, null=True, blank=True, related_name='partprice')
price = models.FloatField(null=True, blank=True)
added = models.DateTimeField(auto_now_add=True, null=True, blank=True)
每个待售商品有4个价格历史记录,按"添加"排序。每个价格旁边都有卖家的名字。
views查询集:
parts = Part.objects.all()
模板:
{% for part in parts %}
{% for y in part.partprice.all|slice:":4" %}
<a href="{{y.part.seller1URL}}"><p>${{y.price}} {{y.seller}}</p></a>
...
...
...
{% endfor %}
{% endfor %}
问题是:
我正在尝试查询:
每个卖家在最新添加日期前订购的每种产品的最后价格
so far I tried:
>>> for part in parts:
... for price in part.partprice.all().order_by('price')[:4]:
... print(price)
结果:
(NGK 3951) $4.0 item1 @2023-01-09 20:36:37.083544+00:00
(NGK 3951) $5.0 item2 @2023-01-09 20:26:12.961078+00:00
(NGK 3951) $5.5 item3 @2023-01-09 20:26:31.890411+00:00
(NGK 3951) $7.0 item4 @2023-01-09 20:26:20.358864+00:00
(Bosch Automotive 9603) $1.0 item4 @2023-01-10 22:21:53.431852+00:00
(Bosch Automotive 9603) $1.0 item1 @2023-01-10 22:22:00.237141+00:00
(Bosch Automotive 9603) $21.0 item3 @2023-01-09 20:26:44.716020+00:00
(Bosch Automotive 9603) $22.0 item1 @2023-01-09 20:26:39.625562+00:00
预期的查询是只显示一次卖方只在每次迭代,如果产品没有价格从任何4个卖家留下空白或只显示最新的价格。
任何帮助都是非常感激的,我希望所有的细节都包括在内。
根据注释…试试这样做:
from django.db.models.aggregates import Max
latest_prices = Price.objects
.values('seller', 'price')
.annotate(latest_report=Max('added'))
我找到的解决方案比我试图完成的问题要简单得多,也要好得多,我只是使用了查询历史
我在价格模型中添加了历史模型,每次我编辑新价格时,历史记录都可以使用这个模板访问:
<td>{% for y in part.partprice.all|dictsort:"price" %} # sort by lowest price
{% if y.seller.name == "seller1" %}
{{y.seller}} {% for x in y.history.all|slice:":1" %}<a href="{{y.part.seller1URL}}">${{x.price}}</a> {{x.history_date|timesince}} Ago<br>{% endfor %} {% for x in y.history.all|slice:"1:2" %} ${{x.price}} {{x.history_date|timesince}} Ago<br>{% endfor %} <hr>
{% endif %}
{% if y.seller.name == "seller2" %}
{{y.seller}} {% for x in y.history.all|slice:":1" %}<a href="{{y.part.seller2URL}}">${{x.price}}</a> {{x.history_date|timesince}} Ago<br>{% endfor %} {% for x in y.history.all|slice:"1:2" %} ${{x.price}} {{x.history_date|timesince}} Ago<br>{% endfor %} <hr>
{% endif %}
{% if y.seller.name == "seller3" %}
{{y.seller}} {% for x in y.history.all|slice:":1" %}<a href="{{y.part.seller3URL}}">${{x.price}}</a> {{x.history_date|timesince}} Ago<br>{% endfor %} {% for x in y.history.all|slice:"1:2" %} ${{x.price}} {{x.history_date|timesince}} Ago<br>{% endfor %} <hr>
{% endif %}
{% if y.seller.name == "seller4" %}
{{y.seller}} {% for x in y.history.all|slice:":1" %}<a href="{{y.part.seller4URL}}">${{x.price}}</a> {{x.history_date|timesince}} Ago<br>{% endfor %} {% for x in y.history.all|slice:"1:2" %} ${{x.price}} {{x.history_date|timesince}} Ago<br>{% endfor %} <hr>
{% endif %}
{% endfor %}
</td>