[Answered ]-Passing Model Field Data to Validators in Django

2👍

Django calls your validator with the value of audio_file. So your isMp3Valid-Validator method receives an instance of django.db.models.fields.files.FieldFile. This class has an attribute called file, which is an django.core.files.base.File.

And the File class has an attribute called name. The value of name should be the absolute path of your audio_file.

Your validator should be changed like this:

def is_mp3_valid(audio_file):
    file_path = audio_file.file.name
    is_valid = False

    f = open(file_path, 'r')
    .... (snip) ...

I hope this helps you.

Leave a comment