我有一個Django項目的問題。「__str__返回的非字符串(類型爲int)」的錯誤
我會直接的。我爲我的Django管理連接到我有一個服務器上的數據庫,問題是,在瀏覽器訪問模型時,會引發以下錯誤:
TypeError at /admin/crm/asentamiento/78967/
__str__ returned non-string (type int)
Request Method: GET
Request URL: http://127.0.0.1:8080/admin/crm/asentamiento/78967/
Django Version: 1.8
Exception Type: TypeError
Exception Value:
__str__ returned non-string (type int)
Exception Location: C:\Spameando\crm_denue2\myvenv\lib\site-packages\django\utils\encoding.py in force_text, line 90
Python Executable: C:\Spameando\crm_denue2\myvenv\Scripts\python3.exe
Python Version: 3.4.4
而對於我的模型代碼是這樣的:
class Asentamiento(models.Model):
id_asentamiento = models.AutoField(primary_key=True)
nom_asentamiento = models.CharField(max_length=150)
tipo_centro = models.CharField(max_length=100)
nom_centro = models.CharField(max_length=100)
num_local = models.CharField(max_length=20)
tipo_asentamiento_id_tipo_asent = models.ForeignKey('TipoAsentamiento', db_column='tipo_asentamiento_id_tipo_asent')
codigo_postal_id_codpostal = models.ForeignKey('CodigoPostal', db_column='codigo_postal_id_codpostal')
localidad_id_localidad = models.ForeignKey('Localidad', db_column='localidad_id_localidad')
class Meta:
managed = False
db_table = 'asentamiento'
def __str__(self):
return self.nom_asentamiento
我不知道是什麼問題,因爲我已經做了很多次模型,我總是拋出相同的錯誤,在所有其他表和模型中,我沒有問題。
當我點擊模型的某些值以在窗口中查看所選值時發生錯誤。
你知道是什麼'__str__'用於? –
'__str__'魔術方法必須返回'str'。你可以用'return str(self.nom_asentamiento)'來解決這個問題' – SethMMorton
相信我,我從一開始就做到了,它並沒有工作,我還認爲這是爲了回報而不是其他東西。 –