【问题标题】:Tree structure with multiples classes association as a successor具有多个类关联的树结构作为后继
【发布时间】:2014-07-16 04:38:37
【问题描述】:

我正在尝试创建一个决策树结构,其中包含多个由一个节点产生的类,但我不知道使用 Django 执行此操作的最佳方法是什么。

为了说清楚,这就是我想要做的(左儿子是条件有效时的情况,右儿子是条件无效时的情况):

           (Condition A)
                |
         -------------------------------
        |                               |
    (Condition B)                 (Condition C)
        |                               |
   ------------------                   |------------
  |                 |                   |           |
(Cond D)    <Category> + <Group>     <Cat>+<Gr>    (Cond D)
  |                                                    |
  ..                                                  ...

这个想法是将一对夫妇 (&lt;Category&gt;,&lt;Group&gt;) 或另一对 &lt;Node&gt; 关联为儿子。问题是,“如何在 Django 中表示多个类字段?”

这是我的模型:

class GroupDecision(models.Model):
    name = models.CharField(max_length=100)
    # Other fields that may come later

class DecisionTree(models.Model):
    name = models.CharField(max_length=100)
    start_node = models.ForeignKey('Node')
    # Other fields that may come later

class Node(models.Model):
    name = models.CharField(max_length=100)
    predecessor = models.ForeignKey('Node', null = True, blank = True, default = None)
    successor = models.ForeignKey('SuccessorAssociation')
    operation = models.ForeignKey('Filter')

class SuccessorAssociation(models.Model):

    TARGET = (('C','Category'),('G','Group'),('N','Node'))

    condition = models.BooleanField()
    target_class = models.CharField(max_length=10,choices=TARGET)
    target_pk = models.IntegerField()

我设法用SuccessorAssociation 来“破解”它,&lt;Category&gt;&lt;Group&gt;&lt;Node&gt;覆盖delete() 方法。

最重要的是,我覆盖了一些由 Django 自己管理的机制。

自定义字段将是解决此问题的一种方法,但我并不真正熟悉它,我认为这是一种不成比例的做法。

有人可以帮我实现吗?

谢谢

【问题讨论】:

    标签: python django model decision-tree


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多