2013-05-07 103 views
4

unicode(self)not working for me。我仍然可以在管理员中看到'名称对象'。我的代码如下:Python 2.7__unicode __(self)not working

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 

感谢你

回答

10

你的问题是,你需要定义在类定义中的__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) 

应该为你工作。

+0

感谢。新手在这里! – 2013-05-07 06:17:12

+0

@ImranOmarBukhsh如果您可以点击答案左侧的投票计数器下的复选标记,我将非常感激。这表明这个问题已经得到了充分的回答,我们都得到了额外的代表! – 2013-05-07 07:36:32

+0

我一直试图做到这一点,只要你把答案,但堆栈溢出不允许我。谢谢。 – 2013-05-08 10:22:55

1

Python的缩进将负责大部分的时间,工作correclty,使用编辑器或分离出_ 的Unicode _(个体经营)与标签

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