2012-07-29 117 views
0

解决这个简单问题的好习惯是什么?最佳做法__unicode __(self)

def __unicode__(self): 
    return unicode(self.typePlace + " " + self.name) 

类型错误:不支持的操作数类型(一个或多个)为+: 'TipoLugar' 和 'STR'

+1

你是什么意思通过“良好做法”?这个问题并不常见。 – madfriend 2012-07-29 20:09:53

回答

1

使用字符串格式化而不是组成,这是更为有效,将字符串化的元素对你来说也是:

return u"%s %s" % (self.typePlace, self.name) 
+0

完美,这是我试图问,我让自己误会。 – cleliodpaula 2012-08-02 21:42:05

7

推测typePlace本身是具有自己__str__()和/或__unicode__()功能的对象(如果它不是,这是一个自定义类,那么你应该添加这些方法)。所以,投typePlace Unicode字符串使用前:

return unicode(unicode(self.typePlace) + " " + self.name)