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
}

最新文章

  1. GitHub使用教程
  2. Codeforces558E A Simple Task(线段树)
  3. ArcGIS支持MongoDB数据源
  4. Android SDK Manager 无法下载更新,或者更新速度超慢,或者待安装包列表不显示
  5. 红字差评系列2.dwarf
  6. ios框架中UIResponder的职责链设计模式应用
  7. Android中颜色的设置
  8. 并发容器之ConcurrentSkipListSet
  9. [转]AIX下调整分区大小
  10. 解决Access restriction问题
  11. start_kernel——local_irq_disable
  12. oracle超出打开游标的最大数的原因和解决方案
  13. Linux 高阶命令进阶(一)
  14. Imcash:坐下来认真和你们说说话
  15. MySQL Hardware--网络测试
  16. Docker集中化web界面管理平台-Shipyard部署记录
  17. java框架之SpringCloud(5)-Hystrix服务熔断、降级与监控
  18. Fiddler怎样抓取手机的包
  19. Python:字符串处理函数
  20. 案例:使用scan IP无法连接数据库

热门文章

  1. Cocos2d-X-3.0 之后的版本的环境搭建
  2. android WIFI信息获取
  3. 项目已经部署,tomcat已经启动,网址也没问题,却出现404错误
  4. StyleBook皮肤控件的使用
  5. 关于页面上输入框中 空格 、0 、NULL 的处理 示例
  6. I.MX6 逻辑分析仪 UART
  7. Are you sure you want to continue connecting etc ssh ssh_config StrictHostKeyChecking no
  8. 关于使用java执行shell脚本获取centos的硬盘序列号和mac地址
  9. socket实现进程间通信(转载)
  10. 在Visual studio 中解除 TFS 的账号绑定