[Django]-How to parse multiple files in multipart form-data with django rest framework?

3👍

A QueryDict has a special function to obtain all values associated with a specific key: getlist(key) [doc]. So you can write it like:

request.data.getlist('image')  # list of images

You can then process every image separately (for example in a for loop).

Or like specified in the documentation:

QueryDict.getlist(key, default=None)

Returns a list of the data with the requested key. Returns an empty list if the key doesn’t exist and a default value wasn’t provided. It’s guaranteed to return a list unless the default value provided is’ a list.

If you perform indexing (like request.data[key]), then Python will call __getitem__ behind the curtains, and this will result in:

QueryDict.__getitem__(key)

Returns the value for the given key. If the key has more than one value, it returns the last value. Raises django.utils.datastructures.MultiValueDictKeyError if the key does not exist. (This is a subclass of Python’s standard KeyError, so you can stick to catching KeyError.)

Leave a comment