0

I need to add a form that has a field that will allow users to upload either documents (.pdf, etc) or images (.png, .jpeg, etc). I know Django has the model fields FileField and ImageField.

How would I create a form that detects the type of upload and uses the correct field type?

Finn
  • 163
  • 6
  • 3
    `FileField` can be used for anything. – xyres Jan 02 '22 at 17:47
  • Thanks xyres. Do you lose any capabilities with just FileField? – Finn Jan 02 '22 at 18:44
  • `ImageField` is just a subclass of `FileField`. The difference is that `ImageField` will also validate the uploaded file. See docs for more: https://docs.djangoproject.com/en/4.0/ref/models/fields/#imagefield. If you're using `FileField`, you'll have to validate the uploaded files yourself. A few years ago, I posted an answer to a similar question where user wanted to only allow images or PDF files to be uploaded. You can find it here: https://stackoverflow.com/q/35465852/1925257 – xyres Jan 02 '22 at 19:01

0 Answers0