我想添加一個快捷方法來簡化我的數據輸出。我寫了下面的代碼(編輯不必要的位):使用模型方法輸入錯誤
address_line_one = models.CharField(max_length=100)
address_line_two = models.CharField(max_length=100, blank=True)
address_city = models.CharField(max_length=50)
address_state = models.CharField(max_length=50)
address_zip = models.IntegerField()
def address(self):
return self.address_line_one+" "+self.address_line_two+" "+self.address_city+" "+self.address_state+" "+self.address_zip
這很簡單。當我運行它時,出現以下錯誤:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/srv/www/energy/energy/customers/models.py", line 83, in address
return self.address_line_two+" "+self.address_city+" "+self.address_state+" "+self.address_zip
TypeError: coercing to Unicode: need string or buffer, long found
爲什麼我得到此錯誤?
如果廣告dress_zip是一個整數,不應該在連接它時將其轉換爲字符串嗎? str(self.address_zip) – 2011-05-08 20:07:19
真的,但不是我的問題的根源(我試過刪除zip之前) – MrGlass 2011-05-08 20:11:31
出於好奇,我決定嘗試「返回'測試'」,我仍然得到這個錯誤。我想我缺少一些方法所需的語法 – MrGlass 2011-05-08 20:15:57