user1958218
user1958218

Reputation: 1621

How can i make the tree like menus from python list

I have the name of files in the list with folders. The list contains 2000 file names like this

Countries/US/newyork/file1.pdf
Countries/Australia/Sydney/file1.pdf
Countries/Canada/Toronto/bla/blabla/file2.pdf

and so on.

I want to index those files in database so that i can have hierarchical directory structure.

IN my Django app i want to display first the root level menus like

countries --- US , Australia, canada

Then i someone click on country then it get the second level of folders and so on and in end i want to see files if there are no more folders.

rather than querying my storage evry time , i want to store all that info in my database so that my web pages are displayed from DB and when user click download then i get the file from my Storage

i am not able to find how should i make the Model or database table for that

Upvotes: 0

Views: 701

Answers (1)

Andrei Kaigorodov
Andrei Kaigorodov

Reputation: 2165

I suggest following way:

Create models to store your tree structure and files for example:

class Node(TreeModel):
    parent # foreign key to Node

class File(Model):
    node # foreign key to Node
    name # name of file
    path # path to the file on disk for example

After that move your files in one or few directories (read this How many files can I put in a directory?) also you can rename them (for example by using hash from files).

Update the model File to put there new paths to your files.

Having done this you are able to easy show files and build path to files etc.

For the model Node use [django-mptt][1] (there are other solutions for django, google it) to get an efficient API to manage a Tree-like model.

You can also create your own Django Storage Backend (or find there are many solutions on the Internet).

Updated

You can add new files by using django admin. You should use amazon s3 django storage backend http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html.

Change:

class File(Model):
    node # foreign key to Node
    name # name of file
    file # django models.FileField

In this case you have not to update index.

Upvotes: 1

Related Questions