How can I determine the order of files/sub-folders within user-generated folders in Django?

I am building a Django web app where users can upload files into a folder structure that they also create/control. I know how to facilitate uploading of files and understand the solution proposed in this question about how to "assign" files (or sub-folders) to their parent folders, without having to create actual folders in the underlying file storage.

However, in my web-app I would also like to allow users to determine the order of files and sub-folders within each folder without relying on the actual filenames.

I know I can set the order of the returned one-to-many model instances by setting the index variable in a meta class (see this question). In my case I could thus insert an "order" field into the file/folder models and then set this value appropriately for each file/sub-folder in a given parent folder. However, this would mean that when a user moves a file from the bottom to the top of the parent folder, for example, every single one of the parent folder's file/sub-folder model instances would need to be edited to reflect their new position in the folder. Maybe my lack of coding experience is misleading me, but this setup looks to me to be error prone and expensive.

Is there a different solution to this problem that is a bit "cleaner"?

Back to Top