如何在django中获取多表继承中的父对象

时间:2015-11-09 22:58:32

标签: django django-models multi-table-inheritance

我有以下两个使用多表继承的模型。 PlayerAchievment扩展了整体成就:

class OverallAchievment(models.Model):
    achievement = models.ForeignKey(Achievement)
    match = models.ForeignKey(Match, limit_choices_to={'week_number': 2})
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)


class PlayerAchievement(OverallAchievment):
    player = models.ForeignKey(Player)

    def __unicode__(self):
        return self.player.first_name + ' ' + self.player.last_name

    def match_detail(self):
        ??? how to get match info here?

我想在PlayerAchievement子函数中提取有关父级匹配属性的一些信息。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

由于PlayerAchievementOverallAchievment延伸,您可以使用其属性:

def match_detail(self):
    self.match.<attr_here>
    ...