Skip to content

Question or Change Request: How to generate a leaf where only specific nodes can be added through the model #12

@ghost

Description


Description

Hi,
i started to use django-polymorphic-tree since two days and it's exactly that what i need for my current project. So at first thank you really for developing it.

I work with django since 5 months so my experience is not progressed. Maybe you can help me get the result that i want.

I would like to descipe a model that has following nodes:

  • Projects
  • Folders
  • Files

With fancytree or jstree I would like to enable the users to create new projects all (on top level). Inside a project the user should be able to create folders and inside a folder the user should be able to create files and so on.

First i created following test for this case:


class BaseTreeNode(PolymorphicMPTTModel):
    parent = PolymorphicTreeForeignKey('self', blank=True, null=True,
                                       related_name='children', verbose_name='parent')
    name = models.CharField(max_length=33)
    uuid = UUIDField(version=4, auto=True)

class Project(BaseTreeNode):
    pass

class Folder(BaseTreeNode):
    pass

class File(BaseTreeNode):
    pass

Test in console

p1 = Project.objects.create(name='Project1')
f1 = Folder.objects.create(name='Folder1', parent=p1)
fi1 = File.objects.create(name='File1', parent=f1)

p2 = Project.objects.create(name='Project2')
f2 = Folder.objects.create(name='Folder2', parent=p2)
fi2 = File.objects.create(name='File2', parent=f2)

p3 = Project.objects.create(name='Project3', parent=fi2)

def walk_the_tree(nodes, level=0):

    for node in nodes:
        print ('\t' * level) + '- <' + node.__class__.__name__ + '>' + node.name
        walk_the_tree(node.get_children(), level+1)

Result

- <Project>Project1
    - <Folder>Folder1
        - <File>File1
- <Project>Project2
    - <Folder>Folder2
        - <File>File2
            - <Project>Project3

In this example Project 3 is added to a file. Currently it's possible but i would like to get an answer from you what is the best solution do handle this case?

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions