Python __str__(self)和__unicode__(self)
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)的更多相关文章
- python __str__ & __repr__ & __cmp__
For ( __str__ ),we going to see a example ... and find who is working for ... #!/usr/bin/python clas ...
- python __str__() 和 __repr__()是干啥的
1. 没定义__str__() print的时候得不到自己想要的东西 类默认转化的字符串基本没有我们想要的一些东西,仅仅包含了类的名称以及实例的 ID (理解为 Python 对象的内存地址即可).虽 ...
- Python __str__函数
class Cat: def __init__(self,_name): self.name = _name def __str__(self): return "i am %s" ...
- python __str__ , __repr__区别
Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数. 函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式 (如果没有等价的语法,则会 ...
- python - __str__ 和 __repr__
内建函数str()和repr() (representation,表达,表示)或反引号操作符(``)可以方便地以字符串的方式获取对象的内容.类型.数值属性等信息.str()函数得到的字符串可读性好(故 ...
- Python __str__(self)
python 在打印一个实例化对象时,打印的是对象的地址,比如:<__main__.Workers object at 0x00000000255A9AC8> 而__str__(self) ...
- python __str__ 和__repr__方法
看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): self.data = value >> ...
- 关于django模型里面的__str__和__unicode__
python3 django模型里面 使用 __str__ 如果使用__unicode__是无效的 简而言之,就是__str__和__unicode__都是为了再管理站点中加载这个表时想显示什 ...
- Python自动化之__unicode__
def __unicode__(self): return u'%s %s' % (self.first_name, self.last_name) 如果定义了__unicode__()方法但是没有定 ...
随机推荐
- my-small.cnf my-medium.cnf my-large.cnf my-huge.cnf
my-small.cnf my-medium.cnf my-large.cnf my-huge.cnf 是 MySQL 默认的几个配置文件.针对不同配置的服务器可以使用不同的配置文件,将你需要的那一个 ...
- 访问某类型的元数据的方式-TypeDescriptor 类
.NET Framework 提供了两种访问某类型的元数据的方式:通过 System.Reflection 命名空间中提供的反射 API,以及通过 TypeDescriptor 类.反射是可用于所有类 ...
- Vi/Vim查找替换使用方法【转】
原文地址:http://wzgyantai.blogbus.com/logs/28117977.html vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂 ...
- <s:property>的用法(jsp获取action中的值或者方法)
1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法) ...
- transport transmission
运输层 transport layer 传输控制协议 transmission control protocol
- XMU 1612 刘备闯三国之桃园结义 【二分】
1612: 刘备闯三国之桃园结义 Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 181 Solved: 12[Submit][Status][We ...
- git lfs
https://git-lfs.github.com/ 1.从这个网址下载git-lfs-windows-amd64-1.1.0.exe,运行这个安装包 2.然后打开git bash 输入git lf ...
- YTU 2573: 连续奇数和
2573: 连续奇数和 时间限制: 1 Sec 内存限制: 128 MB 提交: 63 解决: 37 题目描述 小明看到一本书上写着:任何数字的立方都可以表示为连续奇数的和. 比如: 2^3 = ...
- 安装oh-my-zsh失败,可按以下方式安装
参考:https://jingyan.baidu.com/article/8065f87fae247e2330249876.html .打开 iTerm21. .下载 : git clone git: ...
- word-break word-wrap
work-break:break-all CJK超出的部分自动换行 word-wrap:break-word CJK如果有分隔符,当前分隔符之后与下一个分隔符之间的内容不能在这一行全部显示的话,在当前 ...