Python 2.7__unicode __(self)无效

Imr*_*hsh 4 python django

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
Run Code Online (Sandbox Code Playgroud)

感谢您

Sna*_*fee 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)
Run Code Online (Sandbox Code Playgroud)

应该适合你.