I am using django-rest-framework.
Is there a way to handle multiple file uploading? It seems that even the client is sending multiple files (thourgh a web browser), the MultiPartParser will only select the first file.
I realize this is an old question, but I just spent some time banging my head against this. The issue is that Django's MultiPartParser, which DRF uses, returns files as a MultiValueDict. When DRF then then adds the files back to the data that is passed to the serializer, it does so using .update() on the data (which is an OrderedDict) in request.Request._load_data_and_files(). The result is that if multiple files are uploaded with the same key, only the last one survives [1] and makes it as far as the serializer.
Django's documentation recommends over-riding the .post() method in the FormView if using a form [2]. An alternative is to subclass the parser, and call dict() on the files MultiValueDict before returning it, so that lists are returned instead of being reduced to their last value. I'm using the second option, as I'm subclassing the parser already anyway.
[1] https://docs.djangoproject.com/en/dev/_modules/django/utils/datastructures/
[2] https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#uploading-multiple-files
You may access the file list through request.FILES.getlist('<your_payload_files_key>').
I got the answer from this SO answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With