Django Rest Framework upload file to a method

后端 未结 2 2012
闹比i
闹比i 2020-12-16 03:48

So i have been trying to upload a file to a method using DRF with no luck so far.

I was able to upload to a ModelViewSet using (FormParser, MultiP

2条回答
  •  春和景丽
    2020-12-16 04:37

    Any uploaded files should be available in request.FILES, a dictionary keyed by the field that they was used when uploading. Once you have the file, it's a matter of handling it similar to any other uploaded file in Django.

    If you can, I would use a second serializer that wraps the Multimedia model so the image validation and saving can be done automatically through Django REST Framework. There is an ImageField that will automatically validate the image by Pillow which you can use on the serializer.

提交回复
热议问题