[Answer]-IntegerField automatically set to size of FileField

1👍

You should use FileField.size attribute, which is set by default.

Your file size validator should look like:

def validate_file_size(value):
    if value.size > 1048576:  # Larger than 1MB
        raise ValidationError(u'%s file is too big' % value.name)

You can also rewrite models save() to set your VideoContent.size value when object is saved:

class VideoContent(models.Model):
    ...
    def save(self, *args, **kwargs):
        self.size = self.file.size
        super(VideoContent, self).save(*args, **kwargs)
        self.size = self.file.size
👤niekas

Leave a comment