I would like to have a tree that mimics a file system with folders and files. Folders and files will be defined by different models with different field attributes.
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)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
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)
Shul has:
> Root
> --Download
> ----Test
> --Upload
How can I get this tree in a view and template?
Edit:
Files are not inserted as folder nodes:
file = File.objects.get(filename="Test")
file.get_ancestors()
>>> []
source
share