Django: How do I model a tree of heterogeneous data types?

后端 未结 4 873
刺人心
刺人心 2020-12-15 01:18

I need to store a tree data structure in my database, for which I plan on using django-treebeard or possibly django-mptt. My source of confusion is that each node could be

4条回答
  •  感动是毒
    2020-12-15 02:15

    Your three types are probably easiest handled as FK associations with the fundamental tree.

    The tree can be homogenous -- class MyNode is a direct subclass of treebeard.Node. Your node can have a flag (Root, Middle, Leaf) and FK's for A or B or C. This allows you some SQL-like flexibility in querying MyNode instance.

    This allows your tree to grow. A node can start as a type C (leaf) and then morph to a type B (intermediate). You change the status, and change the FK's.

    The alternative is a bit more complex.

    class MyA( treebeard.Node ):
        pass
    
    class MyB( treebeard.Node ):
        pass
    
    class MyC( treebeard.Node ):
        pass
    

    In this case, you can't "morph" a node. When a node starts as a MyC, and gets children, you have to remove the original MyC instance, and replace it with a MyB version that has a new node as a child. This isn't impossible, but it can be painful.

提交回复
热议问题