'use of property decorator in Django to calculate Sum
I have two moldes in which one model consists of file field. I have to calculate total size of the files in the parent model within the property decorator. I think this could also be done inside the serializer class using the serializer method but I want within the property decorator.
class Package(models.Model):
name = models.CharField(max_length=50,blank=True)
/>....................other fields.........../
**/.........problem is here..../**
@property
def folder_size(self):
all = self.details.all()
all.annotate()
return total
Here I need help to calculate the sum of all files sizes of a single package.
Class Details(models.Model):
package = models.ForeignKey(Package, on_delete=models.CASCADE,null=True,
related_name='details')
notes = models.FileField(blank=True, null=True)
@property
def file_size(self):
return self.file.size
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|