Django rest bookmark loading multiple files

I am using django-rest-framework .

Is there a way to handle multiple files? It seems that even the client sends multiple files (like a web browser), MultiPartParser will only select the first file.

+4
source share
3 answers

If you are going to check several downloaded files, you will have to write your own serializer for this. To check the lists of objects there is serializers.ListField . I have not tried this, but I believe that you can implement a simple serializer as follows:

class FileListSerializer ( serializers.Serializer ) :

    files = serializers.ListField(
                child=serializers.FileField( max_length=100000,
                                             allow_empty_file=False,
                                             use_url=False )
            )

, , , :

files = list( request.FILES.values() )
files_serializer = FileListSerializer( data={"files": files} )
if not file_serializer.is_valid() :
    # handle error
    ...
+2

, , , . , Django MultiPartParser, DRF, files MultiValueDict. DRF , , , .update() ( OrderedDict) request.Request._load_data_and_files(). , , [1] .

Django .post() FormView [2]. dict() files MultiValueDict , . , .

[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

+1

request.FILES.getlist('<your_payload_files_key>').

SO-.

0

Source: https://habr.com/ru/post/1570036/


All Articles