【问题标题】:Python 2.7__unicode__(self) not workingPython 2.7__unicode__(self) 不工作
【发布时间】:2013-05-07 06:14:21
【问题描述】:

unicode(self) 不适合我。我仍然可以在管理员中看到“名称对象”。我的代码如下:

import datetime # standard python datetime module
from django.db import models # Djangos time-zone-related utilities
from django.utils import timezone

class Name(models.Model):
    name = models.CharField(max_length=200)

def __unicode__(self):  # Python 3: def __str__(self):
    return self.name

谢谢你

【问题讨论】:

    标签: python django


    【解决方案1】:

    您遇到的问题是您需要在类定义中定义__unicode__ 方法。

    import datetime # standard python datetime module
    from django.db import models # Djangos time-zone-related utilities
    from django.utils import timezone
    
    class Name(models.Model):
        name = models.CharField(max_length=200)
    
        def __unicode__(self):  # Python 3: def __str__(self):
            return str(self.name)
    

    应该适合你。

    【讨论】:

    • @ImranOmarBukhsh 如果您可以单击我答案左侧投票柜台下的复选标记,我将不胜感激。这表明这个问题已经得到了充分的回答,我们都得到了额外的代表!
    • 我试图在您提出答案后立即执行此操作,但堆栈溢出不允许我这样做。谢谢。
    【解决方案2】:

    Python INDENTION 将负责大部分时间,为了正确工作,使用编辑器或使用标签分隔 _unicode_(self)

          def __unicode__(self):  # Python 3: def __str__(self):
                return str(self.name)
    

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 1970-01-01
      • 2019-09-13
      • 2011-01-19
      • 1970-01-01
      • 2019-06-14
      • 2020-11-04
      • 2016-07-30
      • 2021-12-06
      相关资源
      最近更新 更多