我在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