Python 2.7__unicode __(self)无效

时间:2013-05-07 06:14:21

标签: python django

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 self.name

感谢你

2 个答案:

答案 0 :(得分: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)

应该适合你。

答案 1 :(得分:1)

_ unicode _(自我)与标签分开

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