我在Django中发现错误,说Caught TypeError while rendering: sequence item 1: expected string or Unicode, Property found
。这里是我的代码:Python字符串格式化返回一个Property而不是字符串(unicode)?
def __unicode__(self) :
return "{} : {}".format(self.name, self.location)
我甚至尝试
def __unicode__(self) :
return unicode("{} : {}".format(self.name, self.location))
,但同样的错误。
从我所知道的"this is x = {}".format(x)
返回一个字符串对吗?为什么Python说它是一个属性?
全码:
class Item(models.Model) :
def __unicode__(self) :
return "{} : {}".format(self.name, self.location)
name = models.CharField(max_length = 135)
comment = models.TextField(blank = True)
item_type = models.ForeignKey(ItemType)
location = models.ForeignKey(Location)
t_created = models.DateTimeField(auto_now_add = True, verbose_name = 'created')
t_modified = models.DateTimeField(auto_now = True, verbose_name = 'modified')
class Location(models.Model) :
def __unicode__(self) :
locations = filter(None, [ self.room, self.floor, self.building ])
locations.append(self.prop)
return ", ".join(locations) # This will look in the form of like "room, floor, building, property"
comment = models.TextField(blank = True)
room = models.CharField(max_length = 135, blank = True)
floor = models.CharField(max_length = 135, blank = True)
building = models.CharField(max_length = 135, blank = True)
prop = models.ForeignKey(Property)
t_created = models.DateTimeField(auto_now_add = True, verbose_name = 'created')
t_modified = models.DateTimeField(auto_now = True, verbose_name = 'modified')
class Property(models.Model) :
def __unicode__(self) :
return self.name
name = models.CharField(max_length = 135)
看起来像是说'self.name'是它不能变成字符串的东西。你能说明'self.name'是如何定义的吗? – 2012-03-08 22:59:34
提供的完整代码:-)。 – hobbes3 2012-03-08 23:04:22
@ hobbes3:显然*不是*完整的代码(最重要的是,模块'模型'丢失)。 – Philipp 2012-03-08 23:23:25