我如何确定Django模型中的类实例是否是另一个模型的子类? [英] How can I determine if instance of class from Django model is subclass of another model?
问题描述
我有一个叫$ code> BankAccount 的类作为基类。我还有 CheckingAccount
和 SavingsAccount
继承自 BankAccount
。
I have a class called BankAccount
as base class. I also have CheckingAccount
and SavingsAccount
classes that inherit from BankAccount
.
BankAccount不是一个抽象类,但我不会创建一个对象,只能继承类。
BankAccount is not an abstract class but I do not create an object from it, only the inheriting classes.
然后,我执行这样的查询:
Then, I execute a query like this:
account = BankAccount.objects.get(id=10)
如何知道帐户是否为 CheckingAccount
或 SavingsAccount
?
How do I know if account is CheckingAccount
or SavingsAccount
?
现在我这样做的方式是这样的:
The way I do this now is in this way:
checking_account = CheckingAccount.objects.get(id=account.id)
如果存在,它是一个 CheckingAccount
,否则它是一个 SavingsAccount
。 p>
If it exists, it is a CheckingAccount
, otherwise, it is a SavingsAccount
.
推荐答案
尝试使用 checkingaccount
和 / code>属性。一个不会爆炸的。
Try to use the checkingaccount
and savingsaccount
attributes. The one it is will not blow up.
这篇关于我如何确定Django模型中的类实例是否是另一个模型的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!