Django - 在管理页面中显示"模型对象"而不是对象标题

try*_*arn 3 django unicode model admin

如图所示,它显示"Lecture Object"而不是Lecture的标题.正如我所理解的那样,unicode应该照顾好这个,但它似乎并不存在.

这是我的unicode方法:

def __unicode__(self):
    return self.title
Run Code Online (Sandbox Code Playgroud)

Goc*_*cht 11

要将自定义字符串显示为Model的对象表示,您应该:

在Python 2.x中

def __unicode__(self):
    return self.some_attr  # What you want to show
Run Code Online (Sandbox Code Playgroud)

在Python 3.x中

def __str__(self):
    return self.some_attr  # What you want to show
Run Code Online (Sandbox Code Playgroud)

  • 啊,我明白了!我使用的是 python 3,所以我应该使用 __str__。感谢那。 (2认同)