Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4555888
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:17:21+00:00 2026-05-21T17:17:21+00:00

I’ve been banging my head against the desk for a couple weeks on this

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:17:22+00:00Added an answer on May 21, 2026 at 5:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.