Python 2.7__unicode __(self)不工作 [英] Python 2.7__unicode__(self) not working
本文介绍了Python 2.7__unicode __(self)不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
unicode (自)不适用于我。我仍然可以在管理员中看到名称对象。我的代码如下:
import datetime#standard python datetime module
from django.db import models#Djangos time -zone-related utility
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
感谢您
解决方案
您所遇到的问题是您需要定义 __unicode __
方法在类定义中。
import datetime#standard python datetime module
/ pre>
from django.db import models#Djangos时区相关工具
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)
应该适合你。
unicode(self) is not working for me. I can still see 'Name Object' in the admin. My code is as follows:
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
Thanking you
解决方案The problem you have is that you need to define the
__unicode__
method within the class definition.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)
should work for you.
这篇关于Python 2.7__unicode __(self)不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文