I think the problem you're facing is that you're trying to concatenate a string and an object together. Before I dive into the explanation, you might want to bear a couple of things in mind:
1) Your unicode method doesn't explicitly return a unicode object which will be a problem if any of the variables have characters which are outside of the ASCII range 2) Concatenating strings in python is slow. Use string interpolation instead (see below). Use the following for your unicode method and it should work fine: def __unicode__(self): return u'"%s on device %s" % (self.name, self.server) The reason it works when you specify self.server.name is becuase self.server.name is a unicode object and can be concatenated with a string. In my example, the %s on the device object forces a call to the __unicode__ method on the Device instance. Hope this helps, Euan On 9 July, 02:25, Victor Hooi <victorh...@gmail.com> wrote: > heya, > > I have a small Django app that contains a list of Df (disk-free) > reports. > > "DfReport" has a ForeignKey to "Filesystem" model, which has a > ForeignKey to "Device". (i.e. Devices has filesystems, each filesystem > has a DfReport). > > I'm getting an error in the admin when I attempt to display the > list_display page for a "DfReport" model. > > TemplateSyntaxError at /admin/report/dfreport/add/ > Caught TypeError while rendering: coercing to Unicode: need string > or buffer, Device found > > I've traced the error to the __unicode__ method for one of the > ForeignKey fields on DfReport being displayed. > > The issue is, Filesystem has __unicode__ set to print the server on > which it's reporting. Using the following causes the > TemplateSyntaxError: > > class Filesystem(models.Model): > name = models.CharField(max_length=50) > #description = models.CharField(max_length=100) > description = models.TextField() > server = models.ForeignKey(Device) > > def __unicode__(self): > return self.name + ' on device ' + self.server > > However, if I change self.server to "self.server.name", it works. The > weird thing is - Device (which server points to), has a __unicode__ > method defined as well. Shouldn't calling self.server just use the > __unicode__ method on Device, instead of needing me to explicitly call > self.server.name? > > class Device(models.Model): > name = models.CharField(max_length=50) > #description = models.CharField(max_length=100) > description = models.TextField() > location = models.ForeignKey(Location) > > def __unicode__(self): > return self.name > > class Meta: > ordering = ['name'] > > Or have I got a wrong understanding of how this works? > > Cheers, > Victor -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.