Polymorphism in Django

前端 未结 8 960
死守一世寂寞
死守一世寂寞 2021-01-07 06:11

I have the following models. How do I get access to the unicode of the inheriting tables (Team and Athete) from the Entity table? I\'m trying to display a l

8条回答
  •  梦毁少年i
    2021-01-07 07:09

    From pure Python, you can use the isinstance function:

    class Entity:
        def __init__(self):
            if isinstance(self, Team):
                print 'is team'
            elif isinstance(self, Athlete):
                print 'is athlete'
    
    class Team(Entity):
        def __unicode__(self):
            return 'Team'
    
    class Athlete(Entity):
        def __unicode__(self):
            return 'Athlete'
    

提交回复
热议问题