例如,我有一个项目,“价格”为10,“已付”为2,“运费”为2。我目前正在做以下工作:
@property
def profit(self):
if self.soldprice is not None and self.paid is not None and self.shipcost is not None:
return self.soldprice - self.paid - self.shipcost回报应该是6,(10-2-2)。然后,我在一个模板中调用该利润属性。
{% for inventory in inventory %}
<tr>
<td><a class='btn btn-success btn-sm' href=''>View Breakdown</a>
<td>{{inventory.id}}</td>
<td>{{inventory.product}}</td>
<td>{{inventory.description}}</td>
<td>{{ inventory.profit }}</td>
</tr>
{% endfor %}所以我看到了以下几点:
ID Product Profit
----------------------------------------
6 dessert 8.80
7 bowls 3.37
8 bowls 16.32
15 chip 6.19在调用“利润”属性之后,添加“利润”列的最佳方法是什么?例如,上面的总数是34.68,所以我可以在模板中显示。所有的帮助都是非常感谢的。
发布于 2022-11-09 20:51:18
因为利润没有存储在数据库中,所以不能在ORM查询中增加利润。
但是,您可以很容易地在视图中遍历它,并将其添加到模板的上下文中,前提是库存是一个记录集:
running_total = 0
for i in inventory:
running_total += i.profit
context['total_profit'] = running_total然后将其包含在表/模板的底部,作为{{total_profit}}
https://stackoverflow.com/questions/74380555
复制相似问题