I have a model below and I want to add a custom filesize validator for the file field.
#../MOU.py
def validate_file_size(value):
limit = 1 * 1024 * 1024 # 1 MB
if value.size > limit:
raise ValidationError(_('...') % {
'limit': filesizeformat(limit),
'size': filesizeformat(value.size),
})
class MOU(models.Model):
file = models.FileField(
upload_to='mou/',
null=True,
validators=[
FileExtensionValidator(allowed_extensions=['doc', 'docx', 'pdf']),
validate_file_size
]
)
On doing migrate
I got AttributeError: type object 'MOU' has no attribute 'validate_file_size''
. I’m confused as to why I received this error, any pointers is appreciated.
I can remove the error by creating a separate file for the validator but I want to keep things tidy by having the validators in the same file as the model.