djangodjango-formsdjango-file-upload

How to limit file types on file uploads for ModelForms with FileFields?


My goal is to limit a FileField on a Django ModelForm to PDFs and Word Documents. The answers I have googled all deal with creating a separate file handler, but I am not sure how to do so in the context of a ModelForm. Is there a setting in settings.py I may use to limit upload file types?


Solution

  • I handle this by using a clean_[your_field] method on a ModelForm. You could set a list of acceptable file extensions in settings.py to check against in your clean method, but there's nothing built-into settings.py to limit upload types.

    Django-Filebrowser, for example, takes the approach of creating a list of acceptable file extensions in settings.py.

    Hope that helps you out.