Django multi-table inheritance, how to know which is the child class of a model?

前端 未结 4 2149
梦毁少年i
梦毁少年i 2021-02-19 03:13

I\'m having a problem with multi-table inheritance in django.

Let’s make an example with bank accounts.

class account(models.Model):
    name = models……         


        
4条回答
  •  余生分开走
    2021-02-19 04:07

    To the best of my knowledge there isn't a Django built-in way to do this.

    However, given acc=account.object.get(pk=29), you can use:

    try:
        typeA = acc.accounttypeA
        # acc is typeA
    except accounttypeA.DoesNotExist:
        # acc should be typeB if account only has typeA and typeB subclasses
    
    try:
        typeB = acc.accounttypeB
        # acc is typeB
    except accounttypeB.DoesNotExist:
        # acc should be typeA if account only has typeA and typeB subclasses
    

提交回复
热议问题