我在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 )
self.name
是无法转换为字符串的东西。你能展示一下self.name
是如何定义的吗? - Mike DeSimonemodels
模块)。 - Philipp