Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django rest framework uploading multiple files

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.

like image 636
HanXu Avatar asked Jun 23 '26 06:06

HanXu


2 Answers

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

like image 166
simon Avatar answered Jun 25 '26 19:06

simon


You may access the file list through request.FILES.getlist('<your_payload_files_key>').

I got the answer from this SO answer.

like image 25
chubao Avatar answered Jun 25 '26 19:06

chubao