That's because when you define unicode(), inside the scope of that
function, the name "unicode" is now bound to the function you just
defined.  So, the line "date = unicode(self.dated)" is calling
Letter.unicode() instead of Python's builtin unicode(), and you get an
(almost) infinite recursive loop--"almost" because Python is smart
enough to die after a while (usually with: "RuntimeError: maximum
recursion depth exceeded").

-Jeff

On Oct 23, 8:54 am, Brian <[EMAIL PROTECTED]> wrote:
> I should be more precise. :) Here's a simplified excerpt of the
> problematic code - when a Django (1.0) template tries to turn it into
> text with its default unicode function, Python (2.5.1) crashes (on Mac
> OS X 10.5):
>
>   from django.db import models
>   from people import Person
>
>   class Letter(models.Model):
>       to = models.ManyToManyField(Person, related_name="%
> (class)s_related")
>       author = models.ForeignKey(Person)
>       content = models.TextField()
>
>       def unicode(self):
>           date = unicode(self.dated)
>           author = self.author
>           to = ", ".join([ unicode(r) for r in self.to.all()])
>           return "Letter dated %s from %s to %s" % (date, author, to)
>
>       @models.permalink
>       def get_absolute_url(self):
>           return ('letter_detail', [self.pk])
>
> The fix being, of course, changing "unicode" to "__unicode__".
>
> On Oct 22, 10:14 pm, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
>
> > On Wed, Oct 22, 2008 at 9:45 PM, Brian <[EMAIL PROTECTED]> wrote:
>
> > > This might seem obvious, and I just spend a couple hours straining
> > > over it, so I thought I'd share.
>
> > > If you have a model, e.g.:
>
> > > def MyModel(models.Model):
> > >    # ...
> > >    # with a unicode function likeso:
> > >    def unicode(self):
> > >       return 'something'
>
> > > This will crash Django, without a stack trace.
>
> > > Of course, the function is supposed to be "def __unicode__(self):",
> > > but it'd have been awful nice to be able to figure that out without
> > > rehashing the entire codebase (it seems like such an innocuous
> > > addition to the model at the time haha).
>
> > > Hopefully this will save someone some pain. :)
>
> > It will crash when you do what?  I don't see a crash, I just see models
> > reverting to being reported as "xzy object" in the Admin.
>
> > Karen
>
>
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to