Django model: Reference foreign key table in __unicode__ function for admin
Posted
by pa
on Stack Overflow
See other posts from Stack Overflow
or by pa
Published on 2010-03-21T03:15:50Z
Indexed on
2010/03/21
3:21 UTC
Read the original article
Hit count: 481
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.
© Stack Overflow or respective owner