如何找到django模型基类的"具体类"

Mr *_*ark 11 python django inheritance django-models

我正在尝试使用模型继承时找到django模型对象的实际类.

一些代码来描述问题:

class Base(models.model):
    def basemethod(self):
        ...

class Child_1(Base):
    pass

class Child_2(Base):
    pass
Run Code Online (Sandbox Code Playgroud)

如果我创建两个Child类的各种对象,并创建一个包含它们的查询集:

Child_1().save()
Child_2().save()
(o1, o2) = Base.objects.all()
Run Code Online (Sandbox Code Playgroud)

我想确定对象在basemethod中是否为Child_1或Child_2类型,我可以通过o1.child_1和o2.child_2访问子对象,但是它会重新获得有关基类中子类的知识.

我想出了以下代码:

def concrete_instance(self):
    instance = None
    for subclass in self._meta.get_all_related_objects():
        acc_name = subclass.get_accessor_name()
        try:
            instance = self.__getattribute__(acc_name)
            return instance
        except Exception, e:
            pass
Run Code Online (Sandbox Code Playgroud)

但它感觉很脆弱,如果我继承更多级别,我不确定会发生什么.

Dan*_*aab 13

Django使用父模型表和子模型表之间的OneToOneField实现模型继承.当你这样做时Base.object.all(),Django只查询Base表,因此无法知道子表是什么.因此,遗憾的是,如果没有其他查询,则无法直接转到子模型实例.

代码段显示了将ContentType字段添加到基本模型的常用方法:

from django.contrib.contenttypes.models import ContentType

class Base(models.Model):
    content_type = models.ForeignKey(ContentType,editable=False,null=True)

    def save(self):
        if(not self.content_type):
            self.content_type = ContentType.objects.get_for_model(self.__class__)
        self.save_base()

    def as_leaf_class(self):
        content_type = self.content_type
        model = content_type.model_class()
        if(model == Base):
            return self
        return model.objects.get(id=self.id)
Run Code Online (Sandbox Code Playgroud)

然后,您可以说if Base.content_type.model_class()确定类型.

是另一个片段,它将自定义管理器添加到组合中.

如您所见,这两种解决方案都有可能非常昂贵.如果您有大量实例,则使用as_leaf_class()方法将需要对每个项目进行一次查询.

相反,如果您有一组已知的子模型,只需单独查询每个模型并将实例聚合到一个列表中.


Jan*_*hko 5

看看django-model-utils中的InheritanceManager - 将它附加到模型会给你具体的子类(至少在第一级):

from model_utils.managers import InheritanceManager

class Base(models.Model):
    objects = InheritanceManager()

# ...

Base.objects.all().select_subclasses() # returns instances of child classes
Run Code Online (Sandbox Code Playgroud)

model-utils需要Django 1.2或更高版本.