2013-05-07 100 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)