Django-mptt 和多个父母?

发布于 2024-11-03 15:00:04 字数 2160 浏览 6 评论 0原文

几个星期以来,我一直在为这个问题而苦苦思索,所以我认为可能是时候寻求一些帮助了。

我正在尝试实现一个数据库结构,其中包含组件零件的分层数据。我的主要问题在于尝试将一个“子组件”分配给另一个“组件”/树。参考下面的示例树 - 我创建和使用程序集 1 和 2 没有问题。但是当我制作程序集 3 时,当我调用子程序集时,我收到多个对象返回错误(我根据我的方式理解)尝试)。

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               

这是我一直在尝试的模型:

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

所以基本上我正在创建非常基本的树并通过 Part FK 链接到更详细的数据。我需要能够查询任何程序集并查看它的后代 - 因此虽然我可以调用程序集 3,但我无法调用多个树中的任何子程序集。

对于我正在做的事情,从树中的任何给定点开始,向下将始终相同,即 1.2 将始终有 1.2.1 的子级,但向上可能会发生变化,即 1.2 可以有 1 的父级和/或 3.

问题在于 Assembly.parent 是一个外键,因为它将它限制为一个值。关于一些解决方案或尝试的事情有什么想法吗?

如果您想查看其他代码或提出问题,请告诉我。这似乎是一个很难解释清楚的话题!谢谢

----编辑----

我发现我需要一个有向无环图(DAG)模型。

我还没有具体的解决方案,但是当我弄清楚或提出 DAG 问题时,我会尝试在此处发布链接。


----编辑2----

django-treebeard-dag

django-dag

我发现这两个非常小的项目。我最初使用的是“treebeard”版本,然后在我的项目中切换到 django-dag。如果有关于基本使用的问题,请随时私信我,我会看看是否可以提供帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-11-10 15:00:04

我认为 Django-mptt 对于这个特定的工作来说是错误的工具。它处理树,数据结构中树的一部分是节点有一个父节点,或者节点是树的根。

树是的通用形式。据我所知,没有任何 Django 应用程序可以帮助您处理这些问题。

您可能不得不求助于维护自己的 ManyToMany 关系并放弃 Django-mptt 的便利性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文