Django Tastypie Deserializing Multipart/Form-Data To Upload File -


i trying upload files via multipart/form-data form , tastypie api , running issues:

my model:

class client(models.model):     account = models.foreignkey(account)     client_image = models.filefield(upload_to=client_image_path, default="/assets/img/default-user-image.png", blank=true, null=true)     client_image_thumb = models.filefield(upload_to=client_image_thumb_path, default="/assets/img/default-user-image.png", blank=true, null=true) 

i using custom deserialize method outlined in tastypie issue#42:

class multipartresource(object):     def deserialize(self, request, data, format=none):         if not format:             format = request.meta.get('content_type', 'application/json')          if format == 'application/x-www-form-urlencoded':             return request.post          if format.startswith('multipart'):             data = request.post.copy()             data.update(request.files)             return data          return super(multipartresource, self).deserialize(request, data, format)      def put_detail(self, request, **kwargs):         if request.meta.get('content_type').startswith('multipart') , \                 not hasattr(request, '_body'):             request._body = ''          return super(multipartresource, self).put_detail(request, **kwargs) 

and here corresponding modelresource:

class clientresource(multipartresource, modelresource):     account = fields.foreignkey(accountresource, 'account')      class meta():         queryset = client.objects.all()         always_return_data = true         resource_name = 'account/clients/client-info'         authorization = accountlevelauthorization()         list_allowed_methods = ['get','post','put','delete','patch']         detail_allowed_methods = ['get', 'post', 'put', 'delete','patch']         authentication = apikeyauthentication()         filtering = {             'username': all,         } 

if post content-type application/json , dont include client_image field, create new client object. indicates models/resources working should.

however, when try use multipart/form-data content type can see gets through deserializer appropriately payload:

------webkitformboundaryp0q7q9djlsvvgwbb content-disposition: form-data; name="{%0d%0a%22account%22"  "/api/v1/account/account-info/21/",  ------webkitformboundaryp0q7q9djlsvvgwbb content-disposition: form-data; name="client_image"; filename="donavan.jpg" content-type: image/jpeg  ------webkitformboundaryp0q7q9djlsvvgwbb-- 

i seeing querydict while debugging, shows inmemoryuploadedfile correctly:

<querydict: {u'client_image': [<inmemoryuploadedfile: donavan.jpg (image/jpeg)>], u'{%0d%0a%22account%22': [u'"/api/v1/account/account-info/21/"']}> 

but keep getting error:

{ error_message: "" traceback: "traceback (most recent call last): file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/tastypie/resources.py", line 202, in wrapper response = callback(request, *args, **kwargs) file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/tastypie/resources.py", line 440, in dispatch_list return self.dispatch('list', request, **kwargs) file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/tastypie/resources.py", line 472, in dispatch response = method(request, **kwargs) file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/tastypie/resources.py", line 1328, in post_list updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs)) file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/tastypie/resources.py", line 2104, in obj_create bundle = self.full_hydrate(bundle) file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/tastypie/resources.py", line 890, in full_hydrate value = field_object.hydrate(bundle) file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/tastypie/fields.py", line 732, in hydrate value = super(toonefield, self).hydrate(bundle) file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/tastypie/fields.py", line 165, in hydrate elif self.attribute , getattr(bundle.obj, self.attribute, none): file "/users/stevewirig/documents/www/vu/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 343, in get raise self.field.rel.to.doesnotexist doesnotexist " }

any ideas broken? in advance!

this happened me when post data without providing necessary fields. fields cannot null must provided while posting.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

node.js - Bad Request - node js ajax post -

uitableview - Create and use custom prototype table cells in xamarin ios using storyboard -