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=)
last_name = models.CharField(max_length=) 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__()方法是在一个对象上调用unicode()时被调用的。因为Django的数据库后端会返回Unicode字符串给model属性,所以我们通常会给自己的model写一个__unicode__()方法。前面的例子也可以更简单地写成:

class Person(models.Model):
first_name = models.CharField(max_length=)
last_name = models.CharField(max_length=) def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)

如果定义了__unicode__()方法但是没有定义__str__()方法,Django会自动提供一个__str__()方法调用__unicode__()方法,然后把结果转换为UTF-8编码的字符串对象。在实际开发中,建议:只定义__unicode__()方法,需要的话让Django来处理字符串对象的转换。

============翻译结束==========================

在Flask里,定义一个Article类的数据模型相应的写法可以写成:

class Article(db.Document):
Title = db.StringField(max_length=, required=True)
SegTitle = db.StringField(max_length=)
Url = db.StringField(max_length=, required=True)
Id = db.StringField(max_length=, required=True)
Summary = db.StringField(max_length=)
Content = db.StringField()
SegContent = db.StringField()
Tags = db.ListField(db.EmbeddedDocumentField(Tag))
StrTags = db.ListField(db.StringField(max_length=))
LabeledTags = db.ListField(db.StringField(max_length=))
CrawledDate = db.DateTimeField()
PostDate = db.StringField()
Source = db.StringField()
OriginalSource = db.StringField() @property
def post_type(self):
return self.__class__.__name__ def __unicode__(self):
return self.Title meta = {
'allow_inheritance': False
}

Python __str__(self)和__unicode__(self)的更多相关文章

  1. python __str__ & __repr__ & __cmp__

    For ( __str__ ),we going to see a example ... and find who is working for ... #!/usr/bin/python clas ...

  2. python __str__() 和 __repr__()是干啥的

    1. 没定义__str__() print的时候得不到自己想要的东西 类默认转化的字符串基本没有我们想要的一些东西,仅仅包含了类的名称以及实例的 ID (理解为 Python 对象的内存地址即可).虽 ...

  3. Python __str__函数

    class Cat: def __init__(self,_name): self.name = _name def __str__(self): return "i am %s" ...

  4. python __str__ , __repr__区别

    Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数. 函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式 (如果没有等价的语法,则会 ...

  5. python - __str__ 和 __repr__

    内建函数str()和repr() (representation,表达,表示)或反引号操作符(``)可以方便地以字符串的方式获取对象的内容.类型.数值属性等信息.str()函数得到的字符串可读性好(故 ...

  6. Python __str__(self)

    python 在打印一个实例化对象时,打印的是对象的地址,比如:<__main__.Workers object at 0x00000000255A9AC8> 而__str__(self) ...

  7. python __str__ 和__repr__方法

    看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): self.data = value >> ...

  8. 关于django模型里面的__str__和__unicode__

    python3    django模型里面 使用 __str__  如果使用__unicode__是无效的 简而言之,就是__str__和__unicode__都是为了再管理站点中加载这个表时想显示什 ...

  9. Python自动化之__unicode__

    def __unicode__(self): return u'%s %s' % (self.first_name, self.last_name) 如果定义了__unicode__()方法但是没有定 ...

随机推荐

  1. GCC编译动态和静态链接库例子

    我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不会被连接到目标代码中,而是 ...

  2. Java面试通关要点汇总整理【终极版】(转载)

    简历篇 请自我介绍 请介绍项目 基础篇 基本功 面向对象的特征 final, finally, finalize 的区别 int 和 Integer 有什么区别 重载和重写的区别 抽象类和接口有什么区 ...

  3. 命题作文:在一棵IPv4地址树中彻底理解IP路由表的各种查找过程

    这是一篇命题作文.近期一直想写点东西,但一直找不到题目.正好收到一封邮件,有人问我Linux路由表的布局问题以及路由缓存的问题,加之前些日子又帮人做了一个片上路由表,所以认为这是个好题目,索性花了多半 ...

  4. 错误 1 无法将程序集“NBear.Data.dll”复制到文件“D:\newbpm\bpm\SureBpm\Bin\NBear.Data.dll”。无法将“D:\newbpm\bpm\SureSoft.WebServiceBaseLib\bin\Debug\NBear.Data.dll”添加到网站。 无法添加文件“Bin\NBear.Data.dll”。 拒绝访问。 D:\..

    错误 1 无法将程序集“NBear.Data.dll”复制到文件“D:\newbpm\bpm\SureBpm\Bin\NBear.Data.dll”.无法将“D:\newbpm\bpm\SureSof ...

  5. TCP Operational Overview and the TCP Finite State Machine (FSM) http://tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF.htm

    http://tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF.htm   http://tcpipgu ...

  6. buildroot的使用简介【转】

    本文转载自:http://blog.csdn.net/flfihpv259/article/details/51996204 buildroot简介 1 Buildroot at a glance 2 ...

  7. YTU 2975: 我的编号

    2975: 我的编号 时间限制: 1 Sec  内存限制: 128 MB 提交: 42  解决: 15 题目描述 建立一个学生链表,每个链表结点含有学生的基本信息,编号和姓名.现在n个学生站成一列,根 ...

  8. 网页音乐播放器javascript实现,可以显示歌词

    可以显示歌词,但是歌词和歌曲都要实现自己下载下来.只能播放一首歌,歌词还得是lrc格式的代码写的很罗嗦,急切希望帮改改CSS的代码​1.代码:<html >    <head> ...

  9. memcached value最大限制只能是1M吗

    关于memcached的value最大是1M的限制很多人都知道,但是如果你以为我还要说这个事情,那你就错了. 之前的老版本确实是只能是1M,但是根据git记录,其实2009年以后的版本这个value最 ...

  10. 【基于libRTMP的流媒体直播之 AAC、H264 解析】

    前文我们说到如何在基于 libRTMP 库的流媒体直播过程中推送 AAC .H264 音视频流.本文以上文为基础,阐释如何对 RTMP 包进行解析.重组得到原始的 AAC 音频帧以及 H264 码流. ...