2012-03-08 100 views
3

我在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) 
+1

看起來像是說'self.name'是它不能變成字符串的東西。你能說明'self.name'是如何定義的嗎? – 2012-03-08 22:59:34

+1

提供的完整代碼:-)。 – hobbes3 2012-03-08 23:04:22

+0

@ hobbes3:顯然*不是*完整的代碼(最重要的是,模塊'模型'丟失)。 – Philipp 2012-03-08 23:23:25

回答

1

Property不是指Python屬性,而是指您的Property類。可能發生的事情是這樣的:

  1. Item.__unicode__被調用。
  2. 它抓住了self.nameself.location
  3. self.name從其__unicode__方法中返回一個unicode字符串。
  4. self.location是外鍵,所以調用Location.__unicode__
  5. 得到self.roomself.floorself.building,它們都有__unicode__返回unicode字符串的方法。
  6. filter看到這些字符串全部爲空,所以locations設置爲[]
  7. self.prop,這是一個Property,被附加到locations
  8. ", ".join(locations)拋出TypeError,因爲Property不是字符串。
  9. str.format呼叫Item.__unicode__捕獲異常並拋出它自己,這就是你所看到的。

解決方案:改變

locations.append(self.prop) 

locations.append(unicode(self.prop)) 

道德:str.format電話str()其參數,但str.join沒有。

+0

很好的解釋!儘管我對這一部分有點困惑。如果你說'self.prop'調用'Property .__ unicode__',那麼不是'Property .__ unicode__'也返回一個unicode字符串,因爲它只返回'self.name'?爲什麼我需要做'unicode(self.prop)'?我假設我總是必須用'ForeignKey'來做這件事,但稍微澄清一下會很好:-)。 – hobbes3 2012-03-08 23:55:42

+0

哎呀!我打算說'Location .__ unicode__'。 – 2012-03-09 13:47:58

0

你有沒有試過?:

def __unicode__(self): 
    return "{name}: {location}".format(name=self.name, location=self.location) 

def __unicode__(self): 
    return "{0}: {1}".format(self.name, self.location) 

def __unicode__(self): 
    return "%s: %s" % (self.name, self.location) 

希望它有幫助:)

+0

沒有一個與他的代碼有任何真正的語義差異,所以如果行爲改變了,這是由於Python中的一個bug。 – Daenyth 2012-03-09 00:13:17

相關問題