Django - 如何创建包含其自身类型集合的模型?

Django - How do I create a model that contains a collection of its own type?

我正在创建的模型中的一个字段用于其自身类型的实例列表。我如何在 Django 中执行此操作?我找不到有关如何执行此操作的任何文档..

这与我所说的类似,但不起作用,因为组件 class 尚未定义(可能还有其他原因)。

class Component(models.Model):
    name = models.CharField()
    description = models.CharField()
    status_ok = models.BooleanField()
    subcomponents = models.ForeignKey(Component)

一个常规的 class 简要演示了这个概念:

class Component:
    def __init__(self, name, description, status_ok, *subcomponents):
        self.name = name
        self.description = description
        self.status_ok = status_ok
        self.subcomponents = []
        for subcomponent in subcomponents:
            if isinstance(subcomponent, Component):
                self.subcomponents.append(subcomponent)
            else:
                raise TypeError(subcomponent)

要引用同一模型,请使用正常的 Python 语法 self 但作为字符串,

Class Component(models.Model):
    name = models.CharField()
    description = models.CharField()
    status_ok = models.BooleanField()
    subcomponents = models.ForeignKey('self')