我正在使用具有以下代码的 models.py 制作一个 django 项目:
class Record(models.Model):
id = models.AutoField(primary_key=True)
class TTAMRecord(Record):
client = models.ForeignKey(TTAMClient)
class TTAMClient(models.Model):
...
class Account(models.Model):
records = models.ManyToManyField(Record)
...
我还有以下代码可以将 a 插入TTAMRecord
到arecords
中Account
:
account = Account.objects.get(...)
client = TTAMClient.objects.create(...)
record = TTAMRecord.objects.create(..., client = client, ...)
account.records.add(record)
我想做(但不能)是在帐户的记录对象中调用客户;例如:
account = Account.objects.get(...)
for record in account.records.all():
client = record.client
...
但是,如果我不允许这样做,因为record
这里存储为Record
(没有客户端)类型而不是TTAMRecord
(有客户端)类型......
知道如何投射物体吗?我想使用更通用Record
的而不是TTAMRecord
用于此处未说明的某些目的...