12

I've been banging my head against the desk for a couple weeks on this problem, so I figure it may be time to seek some help.

I'm trying to implement a database structure which has hierarchical data of parts for assemblies. My main problem lies with trying to assign one "subassembly" to another "assembly"/tree. Refering to the example trees below- I have no problem creating and working with assembly 1 and 2. But when I make assembly 3, I get multiple objects returned errors when I call up the subassemblies (which I understand base on the way I'm attempting).

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've been trying:

#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)

#views.py snippet
def assembly_details(request, assembly_name):
        context_instance=RequestContext(request)
        assembly = Assembly.objects.get(name=assembly_name)
        descendants_list = assembly.get_descendants(include_self=False)
        return render_to_response('assembly_details.html', locals(), context_instance,)

So basically I'm creating very basic trees and linking to the more detailed data through the Part FK. I need to be able to query any assembly and look at it's descendants- so although I can call assembly 3, I can't call any of the children that have been in multiple trees.

For what I'm doing, from any given point in the tree, going down will always be the same, i.e. 1.2 will always have a child of 1.2.1, but going up can change, i.e 1.2 can have parents of 1 and/or 3.

The problem is having the Assembly.parent be a ForeignKey as it limits it to one 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 to be a difficult subject to try to clearly explain! Thanks

----EDIT----

I've figured out that I need a directed a-cyclic graph (DAG) model.

I don't have a specific solution yet, but when I figure it out or ask a DAG question, I'll try to post a link here.


----EDIT 2----

django-treebeard-dag

django-dag

I found these two very small projects. I was working with the "treebeard" version originally, then switched over to django-dag for my project. Feel free to PM with questions about basic usage, and I'll see if I can help.

j_syk
  • 6,511
  • 2
  • 39
  • 56
  • 1
    Have you gotten anywhere with either of those two projects? – mkomitee Aug 03 '11 at 14:45
  • I've been using [django-dag](https://github.com/elpaso/django-dag). Basically I use that as the backbone for my structure and then added my own models to add additional details on a per assembly basis. I just added my basic models to djangosnippets [here](http://djangosnippets.org/snippets/2502/) if you want an example to get started – j_syk Aug 03 '11 at 16:13
  • Why did you switch from django-treebeard-dag to django-dag? I am currently evaluating both packages. – Joseph Turian Oct 27 '11 at 00:57
  • I think with treebeard-dag I was having troubles getting started via the factories. I don't know if it was the way my data was formatted or the code not being robust enough. I was generally frustrated with the topic, and still a beginner programmer, so I went with what I could get working the easiest. – j_syk Oct 27 '11 at 13:20
  • Am facing the exact same issue re parts/assemblies and wondered if you succeeded with django-dag? – PhoebeB Jun 11 '15 at 16:14
  • @PhoebeB I am still using django-dag. But ultimately I do all the functions in loops using regular queries. Which really increases the query count, but performance has been good for my application. I have a Part model and a BillOfMaterial model. The BOM has 3 foreign keys to Part: the top-level assembly, the parent part, and the part for that level. – j_syk Jun 12 '15 at 14:30
  • @j_syk Just saw the django-dag repository on github. It's not well-maintained or updated. It's been 5 years since the original post. Are there no proper available packages for DAGs? Also, how would you compare django-dag to django-mptt? Does it come close in terms of performance? – Rahul Sarma Nov 02 '16 at 12:07
  • @RahulSarma I don't still use django-dag, I just loop over a the related objects from a foreign key to get the "descendants". It adds quite a few queries in some cases, but it's still fast. I do use django-mptt for other purposes, though for whatever reason I need to rebuild the tree pretty often. – j_syk Nov 03 '16 at 18:26
  • @j_syk could you provide some samples on how to use django-dag? I could not find any documentation(the included example does not shed much light) and I don't want to switch to a graph database yet. Also do u know of any alternatives to django-dag? – Rahul Sarma Nov 05 '16 at 09:47

1 Answers1

13

I think Django-mptt is the wrong tool for this particular job. It deals with trees, and part of being a tree in Data Structures is that nodes have one parent, or the node is the root of the tree.

Trees are a generalized form of Graphs. I know of no Django app that'll help you handle those.

You may have to resort to maintaining your own ManyToMany relationships and forgo the convenience of Django-mptt.

Michael Easter
  • 23,733
  • 7
  • 76
  • 107
Evan Porter
  • 2,987
  • 3
  • 32
  • 44
  • 5
    So who's going to write a Graph package? ;) – j_syk Apr 27 '11 at 13:07
  • Well I've discovered that I need a DAG to solve this problem. I've found a couple small projects, but nothing too well maintained or documented. Thanks for the lead, Evan. – j_syk May 02 '11 at 15:37
  • 1
    Not being Django specific, have a look at networkx as a graph library. – unode Feb 16 '12 at 22:47