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 )

      

+3


source to share


2 answers


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

    and self.location

    .
  • self.name

    returns a unicode string from its method __unicode__

    .
  • self.location

    is a foreign key, so it Location.__unicode__

    gets called.
  • It gets self.room

    , self.floor

    and self.building

    , which have methods __unicode__

    that return unicode strings.
  • filter

    sees that these lines are empty, so it is locations

    set to []

    .
  • self.prop

    which is is Property

    appended to locations

    .
  • ", ".join( locations )

    Throws TypeError

    out because it is Property

    not a string.
  • the call str.format

    to Item.__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.

+1


source


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 :)

0


source







All Articles