views:

178

answers:

1

Example models:

class Parent(models.Model):
    name = models.CharField()

    def __unicode__(self):
        return self.name

class Child(models.Model):
    parent = models.ForeignKey(Parent)

    def __unicode__(self):
        return self.parent.name # Would reference name above

I'm wanting the Child.unicode to refer to Parent.name, mostly for the admin section so I don't end up with "Child object" or similar, I'd prefer to display it more like "Child of ". Is this possible? Most of what I've tried hasn't worked unfortunately.

A: 
return u'Child of %s' % unicode(self.parent)

Obviously you've defined a __unicode__() method in the parent that makes sense, right?

Ignacio Vazquez-Abrams
Oh fffffff I see. That works, thanks.Apparently my __unicode__ method wasn't working right (?), I used __str__ and THAT showed up in the admin. Any idea why that is? I thought Django would call __unicode__ by default?
pa
`{{ obj }}` in a template calls `unicode(obj)`. Everything else is left to Python.
Ignacio Vazquez-Abrams