[Answer]-Validate Multiple files in django

1👍

If there are multiple (different) file types in TASK_UPLOAD_FILE_TYPES, the for loop will always raise the exception. Because any one of the file types does not match.

You don’t need to use for because str.endswith accepts a tuple as argument.

>>> 'data.docx'.endswith(('.pdf','.zip','.docx'))
True
>>> 'data.py'.endswith(('.pdf','.zip','.docx'))
False

def validate_file_extension(value):
    if not value.name.endswith(tuple(settings_dev.TASK_UPLOAD_FILE_TYPES)):
       raise ValidationError(u'Error message')

Leave a comment