2013-04-18 29 views
1

我試圖通過的multipart/form-data的形式和Tastypie API來上傳文件和正在運行了一些問題:Django的Tastypie反序列化的multipart/form-data的上傳檔案

我的模型:

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) 

我使用在Tastypie問題#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') and \ 
       not hasattr(request, '_body'): 
      request._body = '' 

     return super(MultipartResource, self).put_detail(request, **kwargs) 

,這裏是我的相應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, 
     } 

如果我使用content-type application/JSON執行POST並且不包含client_image字段,它將成功創建一個新的Client對象。這表明模型/資源正在按照他們應該的方式工作。

然而,當我嘗試使用的multipart/form-data的內容類型,我可以看到它在我的解串器適當獲取與此有效載荷:

------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-- 

我也看到這個的QueryDict在調試時,這顯示InMemoryUploadedFile正確:

<QueryDict: {u'client_image': [<InMemoryUploadedFile: donavan.jpg (image/jpeg)>], u'{%0D%0A%22account%22': [u'"/api/v1/account/account-info/21/"']}> 

,但我不斷收到此錯誤:

{ 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 and 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 " }

任何想法,這可能會被打破?提前致謝!

回答

0

這發生在我發佈數據時沒有提供必要的字段。發佈時必須提供那些不能爲空的字段。

相關問題