For several weeks now, I have been banging my head on the table to understand that the time has come to ask for help.
I am trying to implement a database structure that has hierarchical parts data for assemblies. My main problem is trying to assign one "subassembly" to another "assembly" / tree. Turning to the examples of trees below, I have no problem creating and working with assemblies 1 and 2. But when I do assembly 3, I get several objects that return errors when I call nodes (which I understand based on how I'm trying).
assembly 1: assembly 2: assembly 3: 1.1 2.1 2.1 - 1.1.1 - 2.1.1 - 2.1.1 1.2 2.2 1.2 - 1.2.1 - 2.2.1 - 1.2.1
Here is the model I tried:
#models.py snippet class Part(models.Model): part_name = models.CharField(max_length=30, primary_key=True) description = models.TextField(max_length=500, blank=True, null=True) revision = models.CharField(max_length=10, blank=True, null=True) def __unicode__(self): return u'%s' % (self.part_name) class Assembly(MPTTModel): name = models.ForeignKey(Part) parent = models.ForeignKey('self', null=True, blank=True, related_name='children') def __unicode__(self): return u'%s' % (self.name)
So basically I create very simple trees and bind to more detailed data through Part FK. I need to be able to query any assembly and look at the descendants of it, so although I can call assembly 3, I cannot name any of the children that were on several trees.
For what I do, from any point in the tree, the descent will always be the same, i.e. 1.2 will always have a child 1.2.1, but growth can change, that is, 1.2 can have parents 1 and / or 3.
The problem is that Assembly.parent is a ForeignKey as it limits it to a single value. Any ideas on some solutions or things to try?
Please let me know if you want to see additional code or ask questions. This seems like a difficult subject to try to explain clearly! thanks
---- EDIT ----
I realized that I needed a directed a-cyclic graph (DAG) model.
I donβt have a specific solution yet, but when I find out or ask a DAG question, I will try to post the link here.
---- EDIT 2 ----
django-treebeard-dag
django-dag
I found these two very small projects. At first I worked with the version of "treebeard", and then switched to django-dag for my project. Feel free to PM with basic usage questions and I will see if I can help.