Sum of file size

Viewed 17

I am writing a file storage service. I ran into a problem. I have two model classes: Folder and Data. The Data model determines the file size when uploading. How to make folders automatically count the size of this folder, where the folder size = the sum of the sizes of attached files

My models.py file:

class Folder(models.Model):
    uid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)
    # parent_id = models.AutoField(primary_key=True, null=False)
    name = models.CharField(max_length=255)
    # size = models.IntegerField(blank=True, null=True, default = 0)
    size = Data.objects.all()
    created_at = models.DateField(auto_now=True)

# def get_upload_path(instance, filename):
#     return os.path.join(str(instance.folder.uid),filename)

class Data(models.Model):
    id = models.AutoField(primary_key=True, null=False)
    file = models.FileField(null=True, max_length=255)
    created_at = models.DateField(auto_now =True)
    # user = models.ForeignKey(User, on_delete=models.CASCADE)
    parent_id = models.ForeignKey(Folder, on_delete=models.CASCADE)


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