例如,我有一個商品的“soldprice”價格為 10,“paid”為 2,“shipcost”為 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,所以我可以在模板中顯示它。非常感謝任何和所有幫助。
uj5u.com熱心網友回復:
因為利潤不存盤在資料庫中,所以不能在 ORM 查詢中聚合它。
但是,您可以很容易地在視圖中回圈遍歷它并將其添加到模板的背景關系中,假設庫存是一個記錄集:
running_total = 0
for i in inventory:
running_total = i.profit
context['total_profit'] = running_total
然后只需將其包含在表格/模板的底部即可{{total_profit}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/530971.html
