the "Children" of nested files

Viewed 16

I'm doing a project (on Django REST Framework) dedicated to file storage. The project structure assumes that folders can be nested in other folders. And files can be nested in folders. I have prescribed the logic of the file link to the folder in which it is uploaded. However, I need that when I request the service via the API, I am given another field that includes the data of all subfolders and files (the "children") field.

My model.py file:

FILE='FILE'
FOLDER='FOLDER'
TYPE_CHOICES = [(FILE,"FILE"),(FOLDER,"FOLDER")]




class Folder(models.Model):
    id = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)
    parent_id = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)
    type = models.CharField(max_length=255, choices=TYPE_CHOICES, editable=False, default='FOLDER')
    name = models.CharField(max_length=255)
    # size = models.IntegerField(blank=True, null=True, default = 0)
    date = models.DateField(auto_now=True)



class File(models.Model):
    type = models.CharField(max_length=255, choices=TYPE_CHOICES, editable=False, default='FILE')
    id = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)
    file = models.FileField(null=True, max_length=255)
    date = models.DateTimeField(auto_now =True)
    # user = models.ForeignKey(User, on_delete=models.CASCADE)
    parentId = models.ForeignKey(Folder, on_delete=models.CASCADE)
    


    def __str__(self):
        return str(self.file.name)
0 Answers
Related