2
查了一些其他的問題,我想我的tastypie資源應該是這個樣子:使用Backbone將文件上傳到tastypie?
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)
class ImageResource(MultipartResource, ModelResource):
image = fields.FileField(attribute="image")
請告訴我,如果這是錯誤的。
假設上面是正確的,我沒有得到什麼傳遞給資源。這裏是一個文件輸入:
<input id="file" type="file" />
如果我有一個骨幹模型IMG什麼我設置圖像?
img.set("image", $("#file").val()); // tastypie doesn't store file, it stores a string
img.set("image", $("#file").files[0]); // get "{"error_message": "'dict' object has no attribute '_committed'" ...
我怎麼把我的脊樑「圖像」屬性,這樣我可以上傳文件通過Ajax來tastypie?
我仍然感到困惑,我傳入model屬性以便接收文件?或者我總是必須將模型鏈接到表單? – Derek 2013-04-29 20:52:03
我已經用我的上傳處理方式更新了答案。 – 2013-04-30 06:15:34