2012-03-08 127 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

相关问题