Casting objects in django model

后端 未结 2 1361
青春惊慌失措
青春惊慌失措 2021-01-25 14:25

I am making a django project with models.py having following code:

class Record(models.Model):
    id = models.AutoField(primary_key=True)

class TTAMRecord(Reco         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-25 14:50

    As Record is not abstract model, it has its own table and life cycle as other models. However, you can access the corresponding client object as record.ttamclient, so you can change your line to

    account = Account.objects.get(...)
    for record in account.records.all():
        client = record.ttamclient
        ...
    

    However, this is little cumbersome if you have multiple derived classes. In such cases you would have to know which derived class you are referring to and use the corresponding attribute name.

提交回复
热议问题