简体   繁体   English

Django:从html中的相关模型按日期获取最后一个值

[英]Django: Getting last value by date from related model in html

I Just had a question:我只是有一个问题:

So on my parent product overview I show the name of the product and it's URL but there is also a related model to that parent model which holds the history of the prices over time.因此,在我的父产品概述中,我显示了产品的名称及其 URL,但也有一个与该父模型相关的模型,它保存了一段时间内的价格历史记录。

What I want to do is show also the latest price in my table here is my code:我想做的是在我的表格中显示最新价格,这是我的代码:

models.py:模型.py:

class Product(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(max_length=100, default=None, null=True, blank=True)
    url = models.CharField(max_length=255, default=None, null=True, blank=True)
    creation_date = models.DateTimeField(auto_now_add = True)
    update_date = models.DateTimeField(auto_now = True)


class PriceHistory(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    product_info  = models.ForeignKey(Product, on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=20, decimal_places=2)    
    creation_date = models.DateTimeField(auto_now_add = True)

My views.py我的观点.py

@login_required
def overview(request):
    all_products = Products.objects.filter(user=request.user)
    general_context = {'all_products': all_products}
    return render(request, 'overview.html', general_context)    

and my html和我的html

    <h4>Overview Products</h4>
        <table>
          <thead>
            <tr>
              <th>#</th>
              <th>Name</th>
              <th>Url</th>
              <th>Last price</th>
            </tr>
          </thead>
          <tbody>
            {% for product in all_products %}   
                <tr>                    
                <td>{{ forloop.counter }}</td>
                <td>{{ product.name }}</td>
                <td>{{ product.url }}</td>
                <td>{{ product.id.price}}</td>
                <td></td>
                {% endfor %}
              </tr>
          </tbody>
        </table>


How to get the latest price also together with my product in my html overview?如何在我的 html 概览中获得最新价格以及我的产品?

I tried with {{ product.id.price}}.我试过 {{ product.id.price}}。 I think I'm not far off but can't get my head around it.我想我离得不远,但我无法理解它。

You can get the most recent price history using the reverse accessor, ordering by the creation date and then selecting the first object in the queryset:您可以使用反向访问器获取最新的价格历史记录,按创建日期排序,然后选择查询集中的第一个对象:

latest_price_history = product.pricehistory_set.order_by('-creation_date').first()

As to how you get this into your template, you could add this as a model method like so:至于如何将其添加到模板中,您可以将其添加为模型方法,如下所示:

class Product(models.Model):
   ...

   @property
   def latest_price(self):
      return self.pricehistory_set.order_by('-creation_date').first().price

Then in your template you can call it by:然后在您的模板中,您可以通过以下方式调用它:

{{ product.latest_price }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM