How to set different models in the same tree

383 views
Skip to first unread message

Max Demars

unread,
Jun 2, 2014, 11:28:28 AM6/2/14
to django-...@googlegroups.com
Hi,

I think this is something that is missing from the documentation.

I would like to have a tree that would mimic a file system with folders and files. Folders and files would be defined by different models with different fields attributes. Folder and File are two different models with different attrobites.


models:

    from mptt.models import MPTTModel, TreeForeignKey

    class Folder(MPTTModel):
        parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
        name = models.CharField(max_length=50)
        type = models.CharField(max_length=50)
   
    class File(MPTTModel):
        parent= TreeForeignKey(Document)
        filename = models.CharField(max_length=255)
        encoding = models.CharField(max_length=20)

Creating some folders and files:

     from shapefile.models import Folder, File
     root = Folder.objects.create(name="Root")
     download = Folder.objects.create(name="Download", parent=root)
     upload = Folder.objects.create(name="Upload", parent=root)
     File.objects.create(filename="Test", encoding="UTF-8", parent=download)

I shoul have such tree I suppose:

    > Root
    > --Download
    > ----Test
    > --Upload

but files are not inserted as folder's children:

    file = File.objects.get(filename="Test")
    file.get_ancestors()
    >>> []

How can I create such tree and retrieve it in view? My aim is to export the tree to json.

Thanks a lot for any cue.

-Max Demars



Max Demars

unread,
Jun 6, 2014, 11:39:03 AM6/6/14
to django-...@googlegroups.com
Hi,

Can someone can confirm that it is possible to combine two different models in the same tree with django-mptt? Otherwise I know that it could be achieved using django-polymorphic-tree.

Thank you

-Max Demars

Richard Dahl

unread,
Jun 6, 2014, 4:30:44 PM6/6/14
to django-...@googlegroups.com
<marginally-informed opinion> I do not believe that django-mptt supports this. </marginally-informed opinion>  If what you trying to do is mimic a filesystem, this would work:

class Folder(MPTTModel):
parent = TreeForeignkey('self', related_name='children', null=True, blank=True)
name = models.CharField()
type = models.CharField()

class File(models.Model):
folder = TreeForeingKey(Folder)
name = models.CharField()
encoding = models.CharField()


But it obviously introduces the need for a recursive function to get both the folders children and folders files in a view.

To avoid any recursion, you could do something like this:

class FileObject(MPTTModel):
parent = TreeForeignkey('self', related_name='children', null=True, blank=True)
name = models.CharField()
type = models.CharField(Choices=(('file', 'file'), ('folder', 'folder'))
encoding = models.CharField(blank=True, null=True)

Though this will require some bit-fiddlying in the FileObject.Save to ensure only FileObjects with a type='file' get an encoding and that all of them do so and to make sure that no files get any children.

To my limited knowledge, with django-mptt (I know nothing of django-polymorphic-tree), it will come down to the least bad option: recursion or application enforce DB integrity.  Personally, if I had to do this with django-mptt, I'd opt for recursion.

Just my $0.02

-richard


--
You received this message because you are subscribed to the Google Groups "django-mptt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-mptt-d...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Demars

unread,
Jun 12, 2014, 8:17:17 AM6/12/14
to django-...@googlegroups.com
Thank you for your $0.02 Richard.

In your first solution, what is the difference between using TreeForeignKey and the ordinary ForeignKey field? I can't use the MPPT models method such get_ancestors() on File instance anyway. Is there something in TreeForeignKey field that could helps recreating the tree if I know that File instances are always nodes of their ForeignKey(Folder).

-Max Demars

Richard Dahl

unread,
Jun 12, 2014, 2:43:30 PM6/12/14
to django-...@googlegroups.com
Max,
the TreeForeignKey uses a widget that shows the tree in the resulting html ala:
Parent
--- Child One
--- Child Two
--- ChildParentOne
------ Child Three

etc....
Reply all
Reply to author
Forward
0 new messages