Using Django Rest Framework, how can I upload a file AND send a JSON payload? -


i trying write django rest framework api handler can receive file json payload. i've set multipartparser handler parser.

however, seems cannot both. if send payload file multi part request, json payload available in mangled manner in request.data (first text part until first colon key, rest data). can send parameters in standard form parameters fine - rest of api accepts json payloads , wanted consistent. request.body cannot read raises *** rawpostdataexception: cannot access body after reading request's data stream

for example, file , payload in request body:
{"title":"document title", "description":"doc description"}
becomes:
<querydict: {u'fileupload': [<inmemoryuploadedfile: 20150504_115355.jpg (image/jpeg)>, <inmemoryuploadedfile: front end lead.doc (application/msword)>], u'{%22title%22': [u'"document title", "description":"doc description"}']}>

is there way this? can eat cake, keep , not gain weight?

edit: suggested might copy of django rest framework upload image: "the submitted data not file". not. upload , request done in multipart, , keep in mind file , upload of fine. can complete request standard form variables. want see if can json payload in there instead.

i send json , image create/update product object. below create apiview works me.

serializer

class productcreateserializer(serializers.modelserializer):     class meta:          model = product         fields = [             "id",             "product_name",             "product_description",             "product_price",           ]     def create(self,validated_data):          return product.objects.create(**validated_data) 

view

from rest_framework  import generics,status rest_framework.parsers import formparser,multipartparser  class productcreateapiview(generics.createapiview):     queryset = product.objects.all()     serializer_class = productcreateserializer     permission_classes = [isadminorisself,]     parser_classes = (multipartparser,formparser,)      def perform_create(self,serializer,format=none):         owner = self.request.user         if self.request.data.get('image') not none:             product_image = self.request.data.get('image')             serializer.save(owner=owner,product_image=product_image)         else:             serializer.save(owner=owner) 

example test:

def test_product_creation_with_image(self):     url = reverse('products_create_api')     self.client.login(username='testaccount',password='testaccount')     data = {         "product_name" : "potatoes",         "product_description" : "amazing potatoes",         "image" : open("local-filename.jpg","rb")     }     response = self.client.post(url,data)     self.assertequal(response.status_code,status.http_201_created) 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -