djangodjango-modelsmulti-table-inheritance

how to get parent object in multi table inheritance in django


I have the following two models that use multi table inheritance. PlayerAchievment extends OverallAchievment:

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?

I want pull some info about the match attribute of the parent in the PlayerAchievement child function.

How do I do that?


Solution

  • Since PlayerAchievement extends from OverallAchievment you can use its attributes:

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