coercing to Unicode: need string or buffer, Connector found

13 views
Skip to first unread message

adelaide_mike

unread,
Nov 4, 2009, 7:10:51 PM11/4/09
to Django users
Hi. Still learning, slowly. This is Django 1.0.2
The relevant models are Unit, Connector and Cable

class Cable(models.Model):
cable = models.CharField(max_length=8, blank=False)
desc = models.CharField(max_length=128, blank=True)
cabletype = models.ForeignKey(Cabletype, blank=True, null=True)
connector_left = models.ForeignKey(Connector,
related_name='cable_left', blank=True, null=True)
connector_right = models.ForeignKey(Connector,
related_name='cable_right', blank=True, null=True)
length = models.DecimalField
(max_digits=8,decimal_places=2,blank=True,null=True)
version = models.ForeignKey(Version, blank=True, null=True)
def __unicode__(self):
return '%s:%s' % (self.cable, self.version)
class Meta:
ordering = ['cable']
unique_together=('cable','version')

class Unit(models.Model):
unit = models.CharField(max_length=8, blank=False)
desc = models.CharField(max_length=128, blank=True)
rack = models.ForeignKey(Rack)
version = models.ForeignKey(Version, blank=True, null=True)
def __unicode__(self):
return '%s-%s' % (unicode(self.rack), self.unit)
class Meta:
ordering = ['rack','unit']
unique_together=('rack','unit')

class Connector(models.Model):
connector = models.CharField(max_length=8, blank=False)
desc = models.CharField(max_length=128, blank=True)
unit = models.ForeignKey(Unit)
connectortype = models.ForeignKey(Connectortype, blank=True,
null=True)
def __unicode__(self):
return '%s-%s' % (unicode(self.unit), self.connector)
class Meta:
ordering = ['unit','connector']
unique_together=('unit','connector')

Attempting my first reportlab report I get this TypeError:

coercing to Unicode: need string or buffer, Connector found

at the last line in the following view (very much a prototype)

<snip>
cables = Cable.objects.all()
for cable in cables:
print "cable=",
cable # prints OK
print "connector_left=", cable.connector_left
#prints OK
p = Paragraph(cable.cable+cable.desc+cable.connector_left,
style)

If I make the last line:
p = Paragrapg(cable.cable+cable.desc,style)

the report is generated correctly.

What does the TypeError want me to change?

Thanks

Mike

Karen Tracey

unread,
Nov 5, 2009, 12:21:52 AM11/5/09
to django...@googlegroups.com
On Wed, Nov 4, 2009 at 7:10 PM, adelaide_mike <mike....@internode.on.net> wrote:
Attempting my first reportlab report I get this TypeError:

 coercing to Unicode: need string or buffer, Connector found

at the last line in the following view (very much a prototype)

<snip>
   cables = Cable.objects.all()
   for cable in cables:
       print "cable=",
cable                                                # prints OK
       print "connector_left=", cable.connector_left
#prints OK
       p = Paragraph(cable.cable+cable.desc+cable.connector_left,
style)

If I make the last line:
p = Paragrapg(cable.cable+cable.desc,style)

the report is generated correctly.

What does the TypeError want me to change?


It wants you to not try to concatenate a Connector with a unicode type. 

You've got:

<type 'unicode'>+<type 'unicode'>+<class 'Connector'>

You can either do:

cable.cable+cable.desc+unicode(cable.connector_left)

or:

'%s%s%s" % (cable.cable, cable.desc, cable.connector_left)

When you use + to concatenate string/unicode objects the operands must be of an appropriate type or they must be something that can be coerced to the right type.  Having a __unicode__ method doesn't allow something to be coerced to unicode, it just implements the unicode representation for the instances of the class.

Karen

 
Reply all
Reply to author
Forward
0 new messages