小编典典

如何在不知道子类名称的情况下访问Django对象的子类?

django

在Django中,当您有一个父类和从其继承的多个子类时,通常可以通过parentclass.childclass1_set或parentclass.childclass2_set访问一个子类,但是如果我不知道我想要的特定子类的名称怎么办?

有没有一种方法可以在不知道子类名称的情况下沿parent-> child方向获取相关对象?


阅读 493

收藏
2020-03-25

共1个答案

小编典典

(更新:对于Django 1.2及更高版本,它可以在反向的OneToOneField关系中遵循select_related查询(从而实现向下的继承层次结构),有一种更好的技术可用,它不需要real_type在父模型上添加字段。它可以在InheritanceManager中使用django-model-utils项目。)

这样做的通常方法是将ParentKey添加到Parent模型的ContentType上,该模型存储正确的“ leaf”类的内容类型。否则,你可能必须对子表进行大量查询才能找到实例,具体取决于继承树的大小。这是我在一个项目中做的事情:

from django.contrib.contenttypes.models import ContentType
from django.db import models

class InheritanceCastModel(models.Model):
    """
    An abstract base class that provides a ``real_type`` FK to ContentType.

    For use in trees of inherited models, to be able to downcast
    parent instances to their child types.

    """
    real_type = models.ForeignKey(ContentType, editable=False)

    def save(self, *args, **kwargs):
        if not self._state.adding:
            self.real_type = self._get_real_type()
        super(InheritanceCastModel, self).save(*args, **kwargs)

    def _get_real_type(self):
        return ContentType.objects.get_for_model(type(self))

    def cast(self):
        return self.real_type.get_object_for_this_type(pk=self.pk)

    class Meta:
        abstract = True

它被实现为抽象基类以使其可重用。你还可以将这些方法和FK直接放在特定继承层次结构中的父类上。

如果你无法修改父模型,则此解决方案将不起作用。在这种情况下,你几乎无法手动检查所有子类。

2020-03-25