|
官方文档: mro”>https://docs.python.org/2.7/reference/datamodel.html?highlight=mro
object. str ( self ) Called by the str() built-in function and by the print statement to compute the “informal” string representation of an object. This differs from repr() in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object.
【译文】通过内嵌方法str()调用,并通过print语句计算对象的“非正式”字符串表示。这跟repr()的区别在于,它不需要是一个合法的python表达式:可以用一种更便捷或简明的表现方式。返回类型必须是一个string对象。
object. unicode ( self ) Called to implement unicode() built-in; should return a Unicode object. When this method is not defined, string conversion is attempted, and the result of string conversion is converted to Unicode using the system default encoding.
【译文】实现unicode()内嵌函数;应该返回Unicode对象。当没有定义这个方法时,取而代之的是string转换,转换的结果是用系统默认编码转化为Unicode。
=========================================================== ==============
str()是Python的一个“魔幻”方法,这个方法定义了当object调用str()时应该返回的值。Django在许多地方使用str(obj)(或者相关方法,unicode(obj)——见下文),比如说在Django管理站点加载一个对象时显示它的值或者作为对象的显示值插入模板中。因此,我们应该总是返回一个友好的,用户可读的字符串作为对象的str。尽管这不是必须的,但还是建议这么做。例如:
class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50)
def __str__(self):
# Note use of django.utils.encoding.smart_str() here because
# first_name and last_name will be unicode strings.
return smart_str('%s %s' % (self.first_name, self.last_name)
unicode()方法是在一个对象上调用unicode()时被调用的。因为Django的数据库后端会返回Unicode字符串给model属性,所以我们通常会给自己的model写一个unicode()方法。前面的例子也可以更简单地写成:
class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50)
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
如果定义了unicode()方法但是没有定义str()方法,Django会自动提供一个str()方法调用unicode()方法,然后把结果转换为UTF-8编码的字符串对象。在实际开发中,建议:只定义unicode()方法,需要的话让Django来处理字符串对象的转换。
=========================================================================
摘自:https://segmentfault.com/q/1010000006121303?_ea=1020085
在Django中,如果用的是Python3的话就只能用__str__方法,如果是Python2的话就使用__unicode__方法。因为更安全一些
具体到Django中,在 Models中定义这个__unicode__(python3为__str__)方法与否,最直接的感受就是你访问admin所看到的内容是否友好。
下面,举个栗子:
class Question(models.Model):
question_text = models.CharField('question text', max_length=200)
pub_date = models.DateTimeField('date published')
class Meta:
verbose_name = 'question'
verbose_name_plural = verbose_name
ordering = ['-id']
def __str__(self):
return self.question_text
上面的models来自django官方教程,在上面的models中,我定义了一个名为Question的models,此时,访问django的后台admin,看到的内容是这样的。

可以发现,Question看到的内容就是我们上面返回的self.question_text(如果你返回的是其它数据,则看到结果的会不同),那么,如果此时将
上述代码注释掉,也就是变成下面这样,会发生什么呢?
class Question(models.Model):
question_text = models.CharField('question text', max_length=200)
pub_date = models.DateTimeField('date published')
class Meta:
verbose_name = 'question'
verbose_name_plural = verbose_name
ordering = ['-id']
#
# def __str__(self):
# return self.question_text
此时,再去后台admin里面查看Question,则变成了这样:

你定义的问题,看到的内容全部变为了Question object,这样的表示方式,对人来说,是相当的不友好的。
所以,以上,就是我所理解的__unicode__的作用,希望对你有所帮助。
官方文档:https://docs.python.org/3.3/howto/pyporting.html#str-unicode
魔法方法:
http://pyzh.readthedocs.io/en/latest/python-magic-methods-guide.html
转自:http://blog.csdn.net/laoniyouxi123/article/details/51163717 |