我的專案有問題。我的模型中有一個總價,它將匯總玩具車內的所有產品,這是模型的屬性:
@property
def total(self):
total = sum( i.price() for i in self.order_item.all())
if self.discount:
discount_price = (self.discount / 100) * total
return int(total - discount_price)
return self.total
self.discount 是訂單的折扣(如果有),而 self.order_item 是訂單內商品的相關名稱。
所以問題是當我嘗試從這個模型中獲取總數時,它給了我一個錯誤:
maximum recursion depth exceeded while calling a Python object
我從模型中獲取總數的代碼是:
i = order_id
amount = get_object_or_404(Order , id = i)
我也從 url 得到 order_id !
那么這里有什么問題。請幫幫我。
uj5u.com熱心網友回復:
你不應該 return self.total,因為你第二次獲取屬性(因此它將繼續獲取屬性)。您應該回傳total,因此:
@property
def total(self):
total = sum( i.price() for i in self.order_item.all())
if self.discount:
discount_price = (self.discount / 100) * total
return int(total - discount_price)
# ↓ use total, not self.total
return total
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/351732.html
下一篇:水平連接資料幀
