我有一个 Django 项目的问题。
我会直接的。我正在将我的 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
我不知道问题是什么,因为我已经多次完成模型并且我总是抛出相同的错误,在所有其他表和模型中我没有问题。
当我单击模型的某些值以在窗口中查看所选值时发生错误。
原文由 Edgar González 发布,翻译遵循 CC BY-SA 4.0 许可协议
只需将
str()
放在这里: