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