簡體   English   中英

模板渲染期間的 TypeError 錯誤

[英]TypeError Error during template rendering

當我在 Order 表中添加 ForeignKeys 時,我遇到了這個錯誤

在此處輸入圖像描述

這是我的桌子

在此處輸入圖像描述

但是當我刪除那些 ForeignKeys 時,它可以正常工作而不會出錯......

在此處輸入圖像描述 在此處輸入圖像描述

...但我也無法訪問其他表

在此處輸入圖像描述

我看不到您的代碼的 rest(從 Orders 類返回的def __str__ )。 但在我看來,您正試圖將 Object 作為字符串返回。

:您希望將創建的 object 的__str__屬性顯示為您的外鍵產品“整個對象”。

但是您不能將 object 'Product' 作為字符串返回。 當你這樣做時,認為這就是你正在做的事情:

def __str__(self):
    return self.product  # <-- This is an the object, not a string or the __str__ property

您將在 memory 中返回 object 地址,並且由於 object 尚不存在,因此它返回 None。

嘗試將您的回報轉換為顯式字符串

def __str__(self):
    return str(self.product) # <-- Returns the __str__ of the FK Object.

或嘗試返回已經是字符串的屬性

def __str__(self):
    return self.product.name # <-- Returns the attribute from the FK Object.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM