无法在Django Admin面板中显示postgres表的内容

时间:2019-07-25 17:36:21

标签: django postgresql

我在AWS上有一个postgres实例,并将其用作django中的数据库。要将postgres中的数据推送到我的Django models.py 中,我做了-

python manage.py inspectdb > models.py

models.py具有此-

class TasteCluster(models.Model):
    id_cluster = models.IntegerField(blank=True, null=True)
    table_cluster_name = models.CharField(max_length=250, blank=True, null=True)
    cluster_description = models.CharField(max_length=250, blank=True, 
null=True)
    def __str__(self):
        return self.id_cluster

现在,当我在django中的管理面板中检查表格时,我可以看到这样的餐桌味道簇-

Select taste cluster to change

ADD TASTE CLUSTER
Action:   Go 0 of 8 selected

TASTE CLUSTER
    632
    644
    643
    639
    619
    614
    665
    621
8 taste clusters

当我单击任何id_cluster时,都会出现此错误-

TypeError at /admin/dbconnect/tastecluster/8/change/
__str__ returned non-string (type int)
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/dbconnect/tastecluster/8/change/
Django Version: 2.2.3
Exception Type: TypeError
Exception Value:    
__str__ returned non-string (type int)
Exception Location: /usr/local/lib/python3.7/site-packages/django/template/defaultfilters.py in _dec, line 42
Python Executable:  /usr/local/opt/python/bin/python3.7
Python Version: 3.7.3
Python Path:    
['/Users/rahman/Desktop/django_exercise/03project/post',
 '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
 '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
 '/Users/rahman/Library/Python/3.7/lib/python/site-packages',
 '/usr/local/lib/python3.7/site-packages',
 '/usr/local/lib/python3.7/site-packages/geos',
 '/usr/local/Cellar/numpy/1.16.3_1/libexec/nose/lib/python3.7/site-packages',
 '/usr/local/Cellar/protobuf/3.7.1/libexec/lib/python3.7/site-packages']
Server time:    Thu, 25 Jul 2019 16:19:03 +0000

如何获取/查看表的所有列和行?

我是django的新人!

1 个答案:

答案 0 :(得分:1)

就像错误说的那样,您的__str__方法不是返回字符串,而是整数。

您应该返回一个实际的字符串;毫无疑问,table_cluster_name最有意义。

def __str__(self):
    return self.table_cluster_name