Python字符串格式化返回一个属性而不是字符串(Unicode)?

3

我在Django中遇到了一个错误,提示Caught TypeError while rendering: sequence item 1: expected string or Unicode, Property found。以下是我的代码:

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是如何定义的吗? - Mike DeSimone
@hobbes3:显然这不是完整的代码(最重要的是,缺少了models模块)。 - Philipp
2个回答

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. Item.__unicode__中的str.format调用捕获该异常并抛出自己的异常,这就是您看到的内容。
解决方案:更改
locations.append( self.prop )

locations.append( unicode(self.prop) )

道理是:str.format会在其参数上调用str(),但str.join不会。

很好的解释!但是有一部分我还是有点困惑。如果你说 self.prop 调用了 Property.__unicode__,那么 Property.__unicode__ 也会返回一个 Unicode 字符串,因为它只是返回了 self.name,那我为什么需要使用 unicode( self.prop ) 呢?我猜我总是需要在 ForeignKey 中这样做,但是能否再解释一下呢 :-)? - hobbes3

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)

希望能够帮到你 :)


这些代码与他的代码没有任何实质性的语义差异,所以如果行为发生变化,那就是Python中的一个错误。 - Daenyth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接