【问题标题】:Django - Display "Model Object" in the admin page instead of Object titleDjango - 在管理页面中显示“模型对象”而不是对象标题
【发布时间】:2016-02-20 10:17:46
【问题描述】:

如图所示,它显示“讲座对象”而不是讲座的标题。据我了解,unicode 应该解决这个问题,但这里似乎没有。

这是我的 unicode 方法:

def __unicode__(self):
    return self.title

【问题讨论】:

  • 你的python版本是多少?在控制台中运行python --version

标签: django unicode model admin


【解决方案1】:

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

在 Python 2.x 中

def __unicode__(self):
    return self.some_attr  # What you want to show

在 Python 3.x 中

def __str__(self):
    return self.some_attr  # What you want to show

【讨论】:

  • 啊,我明白了!我正在使用 python 3,所以我应该一直在使用 str。谢谢你。
猜你喜欢
  • 2020-12-18
  • 2018-04-18
  • 1970-01-01
  • 2011-11-22
  • 2010-12-08
  • 2017-04-25
  • 1970-01-01
  • 2019-07-24
  • 2021-08-01
相关资源
最近更新 更多