在 Python 2 上使用 Django 中的 __str__() 方法
Using __str__() method in Django on Python 2
我正在使用 Django project tutorial 学习 Django。
由于我使用 python 2.7,因此我无法在 python 2.7 中实现以下内容:
from django.db import models
class Question(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.question_text
class Choice(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.choice_text
是的,您只需将 __str__
替换为 __unicode__
,如评论所述:
class Question(models.Model):
# ...
def __unicode__(self):
return self.question_text
class Choice(models.Model):
# ...
def __unicode__(self):
return self.choice_text
在该部分的下方,您会找到一些解释:
__str__
or __unicode__?
On Python 3, it’s easy, just use __str__()
.
On Python 2, you should define __unicode__()
methods returning unicode values instead. Django models have a default __str__()
method that calls __unicode__()
and converts the result to a UTF-8 bytestring. This means that unicode(p)
will return a Unicode string, and str(p)
will return a bytestring, with characters encoded as UTF-8. Python does the opposite: object has a __unicode__
method that calls __str__
and interprets the result as an ASCII bytestring. This difference can create confusion.
question_text
和 choice_text
属性 return Unicode 值已经存在。
为了保持py2和py3的代码兼容,更好的方法是使用装饰器python_2_unicode_compatible
。
这样你就可以保留 str 方法:
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Question(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.question_text
@python_2_unicode_compatible
class Choice(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.choice_text
参考:https://docs.djangoproject.com/en/1.8/topics/python3/#str-and-unicode-methods
Django provides a simple way to define str() and unicode() methods that work on Python 2 and 3: you must define a str() method returning text and to apply the python_2_unicode_compatible() decorator.
...
This technique is the best match for Django’s porting philosophy.
我正在使用 Django project tutorial 学习 Django。 由于我使用 python 2.7,因此我无法在 python 2.7 中实现以下内容:
from django.db import models
class Question(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.question_text
class Choice(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.choice_text
是的,您只需将 __str__
替换为 __unicode__
,如评论所述:
class Question(models.Model):
# ...
def __unicode__(self):
return self.question_text
class Choice(models.Model):
# ...
def __unicode__(self):
return self.choice_text
在该部分的下方,您会找到一些解释:
__str__
or__unicode__?
On Python 3, it’s easy, just use
__str__()
.On Python 2, you should define
__unicode__()
methods returning unicode values instead. Django models have a default__str__()
method that calls__unicode__()
and converts the result to a UTF-8 bytestring. This means thatunicode(p)
will return a Unicode string, andstr(p)
will return a bytestring, with characters encoded as UTF-8. Python does the opposite: object has a__unicode__
method that calls__str__
and interprets the result as an ASCII bytestring. This difference can create confusion.
question_text
和 choice_text
属性 return Unicode 值已经存在。
为了保持py2和py3的代码兼容,更好的方法是使用装饰器python_2_unicode_compatible
。
这样你就可以保留 str 方法:
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Question(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.question_text
@python_2_unicode_compatible
class Choice(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.choice_text
参考:https://docs.djangoproject.com/en/1.8/topics/python3/#str-and-unicode-methods
Django provides a simple way to define str() and unicode() methods that work on Python 2 and 3: you must define a str() method returning text and to apply the python_2_unicode_compatible() decorator.
...
This technique is the best match for Django’s porting philosophy.