On Thu, 2009-04-30 at 12:42 -0700, Bro wrote:
> class SentenceText(models.Model):
>     language = models.ForeignKey(Language)
>     text_id = models.IntegerField()
>     text = models.TextField()
>     def __unicode__(self):
>         return self.id + ' ' + self.text
> 
> class Box(models.Model):
>     label = models.ForeignKey(SentenceText, to_field='text_id',
> related_name='%(class)s_related_label')
>     description = models.ForeignKey(SentenceText, to_field='text_id',
> related_name='%(class)s_related_description')
>     def __unicode__(self):
>         return self.label
> 
> ============================
> 
> I've got a strange error and in many hour, I haven't manage to fix it.
> 
> In the Box class, I have 2 ForeignKey to SentenceText because I want
> for example the same label (or description) in french or in english.
> 
> But when I try to show a Box list (in admin panel) or a Box detail,
> I've got this error :
> 
> "Caught an exception while rendering: coercing to Unicode: need string
> or buffer, SentenceText found"

A  __unicode__ method *must* return a unicode string. You're returning
self.label which is a SentenceText object -- as the exception notes.
It's certainly no doubt possible to convert a SentenceText into a string
by some means, but Python will not do that for you as part of the
__unicode__ (or __str__) method return process. You must make sure you
are converting to a string.

The simplest and normally completely correct solution here is to write

        def __unicode__(self):
           return unicode(self.label)
        
Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~----------~----~----~----~------~----~------~--~---

Reply via email to