Python string formatting return property instead of string (unicode)?
Django gets an error saying Caught TypeError while rendering: sequence item 1: expected string or Unicode, Property found
. Here is my code:
def __unicode__( self ) :
return "{} : {}".format( self.name, self.location )
I even tried
def __unicode__( self ) :
return unicode( "{} : {}".format( self.name, self.location ) )
but the same error.
From what I know, "this is x = {}".format( x )
returns the string to the right? Why does Python say this property?
Complete code:
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 )
source to share
Property
does not refer to a Python property, but refers to your class Property
. This is probably happening:
-
Item.__unicode__
is called. - It captures
self.name
andself.location
. -
self.name
returns a unicode string from its method__unicode__
. -
self.location
is a foreign key, so itLocation.__unicode__
gets called. - It gets
self.room
,self.floor
andself.building
, which have methods__unicode__
that return unicode strings. -
filter
sees that these lines are empty, so it islocations
set to[]
. -
self.prop
which is isProperty
appended tolocations
. -
", ".join( locations )
ThrowsTypeError
out because it isProperty
not a string. - the call
str.format
toItem.__unicode__
catches this exception and throws its own what you see.
Solution: change
locations.append( self.prop )
to
locations.append( unicode(self.prop) )
Moral: str.format
Calls str()
in its arguments, but str.join
doesn't work.
source to share
You tried?:
def __unicode__( self ):
return "{name}: {location}".format(name=self.name, location=self.location)
or
def __unicode__( self ):
return "{0}: {1}".format(self.name, self.location)
or
def __unicode__( self ):
return "%s: %s" % (self.name, self.location)
Hope this helps :)
source to share