python 如何确定 Django 模型中的类实例是否是另一个模型的子类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2315047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:21:11  来源:igfitidea点击:

How can I determine if instance of class from Django model is subclass of another model?

pythondjangoinheritancedjango-modelssubclass

提问by dannyroa

I have a class called BankAccountas base class. I also have CheckingAccountand SavingsAccountclasses that inherit from BankAccount.

我有一个称为BankAccount基类的类。我也有CheckingAccountSavingsAccount类,继承BankAccount

BankAccount is not an abstract class but I do not create an object from it, only the inheriting classes.

BankAccount 不是抽象类,但我没有从中创建对象,只有继承类。

Then, I execute a query like this:

然后,我执行这样的查询:

account = BankAccount.objects.get(id=10)

How do I know if account is CheckingAccountor SavingsAccount?

我怎么知道 account 是CheckingAccount还是SavingsAccount

The way I do this now is in this way:

我现在这样做的方式是这样的:

checking_account = CheckingAccount.objects.get(id=account.id)

If it exists, it is a CheckingAccount, otherwise, it is a SavingsAccount.

如果存在,则为CheckingAccount,否则为SavingsAccount

采纳答案by Ignacio Vazquez-Abrams

Try to use the checkingaccountand savingsaccountattributes. The one it is will not blow up.

尝试使用checkingaccountsavingsaccount属性。它是不会炸毁的。

回答by Esteban Küber

You could use isinstance(account, SavingsAccount), but is generally preferred to avoid itand use duck type inferenceby looking at the object's attributes, and see if it quacks like a subclass.

您可以使用isinstance(account, SavingsAccount),但通常最好避免使用它,并通过查看对象的属性使用鸭子类型推断,看看它是否像子类一样嘎嘎作响。

To see if an object has an attribute, you use the aptly named hasattrbuilt-infunctionor use getattrand check for the raising of an AttributeError exception.

要查看对象是否具有属性,您可以使用恰当命名的hasattr内置函数或使用getattr并检查是否引发 AttributeError 异常。

回答by Fraser Graham

Add a GetAccountType() method to your Checking and Savings accounts, when you get the object back from BankAccount.objects.get() then call that, if everything that derives from BankAccount has that method then you'll be fine.

将 GetAccountType() 方法添加到您的 Checking 和 Savings 帐户,当您从 BankAccount.objects.get() 取回对象时,然后调用它,如果从 BankAccount 派生的所有内容都具有该方法,那么您就可以了。

回答by i41

After some more searching I found solutions akin to this: Django multi-table inheritance, how to know which is the child class of a model?

经过更多搜索,我找到了类似于以下的解决方案: Django 多表继承,如何知道哪个是模型的子类?

Basically, there's no elegant solution for this. You have to do a bunch of try-except statements and force django to use the class you want.

基本上,对此没有优雅的解决方案。你必须做一堆 try-except 语句并强制 django 使用你想要的类。

回答by inkedmn

A little janky, but this would work:

有点笨拙,但这会起作用:

>>> class BankAccount(object): pass
...
>>> class SavingsAccount(BankAccount): pass
...
>>> class CheckingAccount(BankAccount): pass
...
>>> x = SavingsAccount()
>>> type(x) == type(SavingsAccount())
True
>>> type(x) == type(CheckingAccount())
False