Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django-在抽象祖先中实现的方法中的内省Django模型?_Django_Inheritance_Abstract Class - Fatal编程技术网

Django-在抽象祖先中实现的方法中的内省Django模型?

Django-在抽象祖先中实现的方法中的内省Django模型?,django,inheritance,abstract-class,Django,Inheritance,Abstract Class,在django中,我想引用调用其方法的类,在该类中,方法本身是在其内部实现的 当然,在我的方法中,我不知道哪个子模型被称为先验。你为什么这么说?你绝对可以。AbstractFather是一个抽象模型,因此它永远不会被实例化,因此您可以始终确保调用my\u method的任何东西都是子类的实例。您给出的语法应该有效 那么,你到底想和什么做比较呢self将始终是相关实例,其类将始终是AbstractFather的特定子类。您需要检查什么?琐碎且有效: class AbstractFather(mod

在django中,我想引用调用其方法的类,在该类中,方法本身是在其内部实现的


当然,在我的方法中,我不知道哪个子模型被称为先验。

你为什么这么说?你绝对可以。AbstractFather是一个抽象模型,因此它永远不会被实例化,因此您可以始终确保调用
my\u method
的任何东西都是子类的实例。您给出的语法应该有效

那么,你到底想和什么做比较呢<
my_method
中的code>self将始终是相关实例,其类将始终是AbstractFather的特定子类。您需要检查什么?

琐碎且有效:

class AbstractFather(models.Model):
    class Meta:
        abstract = True
    def my_method(self,some_instance):
        print isinstance(some_instance,self.__class__)

class Child(AbstractFather):
    pass

你能澄清一下吗?你是说self.childmethod()不起作用吗?你为什么要这样构造它?我的方法中的自我实际上是孩子的实例,就像抽象父亲一样。。。摘要:)我的意思是,我不应该在AbstractFather中硬编码Child,因为实际上有Child1、Child2等。。。从这个意义上说,一个先天的父亲并不知道自己的孩子。
isinstance(instance, Child):
class AbstractFather(models.Model):
    class Meta:
        abstract = True
    def my_method(self,some_instance):
        print isinstance(some_instance,self.__class__)

class Child(AbstractFather):
    pass