【问题标题】:Django queryset related field lookup with filtering the last objectDjango 查询集相关字段查找过滤最后一个对象
【发布时间】:2023-01-16 00:50:37
【问题描述】:

我正在构建一个价格比较 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 个按“添加”排序的价格历史记录,每个价格旁边都有卖家名称。

查看查询集:

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 %}

问题是:

我正在尝试查询:

每个卖家按最新添加日期订购的每个产品的最后价格

到目前为止我试过:

    >>> 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 个卖家中任何一个的价格,则将其留空或只显示最新的可用价格。

非常感谢任何帮助,我希望包括所有详细信息。

【问题讨论】:

  • 需要明确的是,您正在尝试根据每个供应商的时间戳获取最新价格,对吗?
  • @arcee123 是的 :) 基本上每次发布特定供应商的新价格时,它都会添加到产品价格中,而旧价格会被删除“如果有任何以前的价格”。

标签: python django django-models django-templates django-queryset


【解决方案1】:

根据评论... 尝试这样的事情:

from django.db.models.aggregates import Max
latest_prices = Price.objects 
  .values('seller', 'price') 
  .annotate(latest_report=Max('added'))

【讨论】:

  • 做了改变。对不起,是我脑子里做的。
【解决方案2】:

我找到的解决方案比我试图完成的问题简单得多,也好得多,我只是使用Querying History

我在我的价格模型中添加了历史模型,每次我编辑新价格时,都可以使用此模板访问历史模型:

<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>

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 2012-12-03
    • 2020-06-16
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 2016-07-20
    相关资源
    最近更新 更多