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

后端 未结 7 777
星月不相逢
星月不相逢 2020-12-02 23:31

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 sur

7条回答
  •  半阙折子戏
    2020-12-03 00:06

    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.

提交回复
热议问题