开发者

Django model: Reference foreign key table in __unicode__ function for admin

开发者 https://www.devze.com 2022-12-24 16:17 出处:网络
Example models: class Parent(models.Model): name = models.CharField() def __unicode__(self): retu开发者_JAVA百科rn self.name

Example models:

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

    def __unicode__(self):
        retu开发者_JAVA百科rn 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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号