3

I'm making my own "cloud server" with django, for my projects and files. I'm trying to create tree file structure, but I cant figure out the way to do it.
And how I can make username based URL, like (username/root/home/Documents/...)
I'm also interested of some good links and examples of authentication and django style cloud server solutions.

models.py

class BasicFile(models.Model):
    file_name = models.CharField(max_length=80)
    last_edit = models.DateTimeField(default=datetime.now, blank=True)
    sub_folders = models.IntegerField()
    sub_files = models.IntegerField()

    def __str__(self):
        return self.file_name


views.py

class IndexView(LoginRequiredMixin, ListView):
    template_name = 'cloud/index.html'
    context_object_name = 'project_file'

    def get_queryset(self, *args, **kwargs):
        return ProjectFile.objects.all()



urls.py

re_path(r'^(?P<username>)/$', views.IndexView.as_view(), name='index'),
re_path(r'^(?P<username>/f1/f1_child)/$', views.IndexView.as_view(), name='index'),
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Jertzuuka
  • 163
  • 1
  • 1
  • 5

1 Answers1

1

To be able to create a nested structure from your file objects you could create an optional relationship so that files can have 'parents' which you could then build up a tree from.

To do so you could add a field to your model;

parent = models.ForeignKey("self", blank=True)

This may help with your understanding of authentication & users; https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Authentication

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • @ markwalker_ When i add `parent = models.ForeignKey('self', blank=True, on_delete=models.CASCADE) `,It gives me error about `TypeError: __init__() missing 1 required positional argument: 'to' ` My plan was to make tree data structure with one model, is that even possible? – Jertzuuka May 14 '18 at 16:54
  • Yeah, this would give you a hierarchy using only 1 model. What version of Django are you using because looking at the docs, that's as it states so should be fine; https://docs.djangoproject.com/en/1.11/ref/models/fields/#foreignkey - post the full stacktrace of the error if you can't figure it out. – markwalker_ May 14 '18 at 22:28