我工作的Django應用程序,但我的模型的一個嘗試編輯在管理該領域時,造成must be str, not NoneType
錯誤之一。的Python:__str__method導致「必須海峽,不NoneType」錯誤只爲兩個相同的領域
堆棧跟蹤顯示,原因是我__str__
方法,定義爲:
def __str__(self):
return self.restaurant.name + " ==> Looks for " + self.container_tag + \
" with class: " + self.container_class or "[Not set]" + " , with id: " + self.container_id or "[Not set]"
這裏是因爲在我的模型類中定義的字段:
container_tag = models.CharField(max_length=15)
container_class = models.CharField(max_length=25, null=True, blank=True)
container_id = models.CharField(max_length=25, null=True, blank=True)
正如你可以看到,近兩年完全一樣。當我輸入值到container_class
(而忽略container_id
)並保存,然後一切工作正常。但是,當我輸入值到container_id
而不是container_class
然後我得到上述錯誤。
據我所知or
運營商應該做的工作,只是「忽略」字段,如果它是空的。
爲了完整起見,這裏是觸發錯誤的行:
" with class: " + self.container_class or "[Not set]" + " , with id: " + self.container_id or "[Not set]"
' 「帶班」 + None'會返回一個'TypeError'異常和'__str__'必須返回一個'str'。 –