尝试使用 REST 框架或 Tastypie 将图像上传到 FileField 模型

Trying to upload image to a FileField model using REST framework or Tastypie

谁能给我一步一步的/ link 资源,告诉我如何使用 REST API 将文件从 mobile/desktop 应用程序上传到基于 Django 的服务器?

服务器有一个带有名为 "thumbnail" 的 FileField 的模型。我可以上传其他数据,但文件似乎是个大问题。

请注意,我不是在谈论使用 browser/Django 表单上传,而是通过 Http 请求从应用上传

api:

from models import Article

class ArticleResource(ModelResource):

    class Meta:
        queryset = Article.objects.all()
        resource_name = 'article'
        filtering = {'title': ALL}
        authorization=Authorization()

我用来发出 Http 请求的独立 python 脚本(模拟移动应用程序)

url="http://127.0.0.1:8000/articles/api/article/"

data={
    'title':'Tastypie',
    'body':'First Restful client',
    'pub_date':'05/02/2015',
    }
files=  {'thumbnail': open('django.png', 'rb')}
headers =  {'content-type': 'image/png'}
print requests.post(url, files=files)

型号:

class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    pub_date = models.DateTimeField('date published')
    likes = models.IntegerField(default=0)
    thumbnail = models.FileField(blank=True,null=True,upload_to=get_upload_file_name)
    def __unicode__(self):
        return str(self.title)

编辑:

这有效:

api:

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()
                photo = Article()
                photo.thumbnail = request.FILES['thumbnail']
                photo.title = request.POST.get('title')
                photo.body=request.POST.get('body')
                photo.pub_date = request.POST.get('pub_date')
                photo.save()
                # ... etc
                return data
            return super(ArticleResource, self).deserialize(request, data, format)

        # overriding the save method to prevent the object getting saved twice 
        def obj_create(self, bundle, request=None, **kwargs):
             pass


class ArticleResource(MultipartResource,ModelResource):

    class Meta:
        queryset = Article.objects.all()
        resource_name = 'article'
        filtering = {'title': ALL}
        authorization=Authorization()

Http 请求Python脚本:

url="http://127.0.0.1:8000/articles/api/article/"

data={
    'title':'Tastypie',
    'body':'First Restful client',
    'pub_date':'2015-02-05',
    }
files=  {'thumbnail': open('django.png', 'rb')}

print requests.post(url, data=data, files=files).text

Tastypie 没有任何保存二进制文件的好方法。我会尝试这样的事情:

class ArticleResource(ModelResource):

    class Meta:
        queryset = Article.objects.all()
        resource_name = 'article'
        filtering = {'title': ALL}
        authorization=Authorization()    

    # save the photo
        def deserialize(self, request, data, format=None):
            if not format:
                format = request.META.get('CONTENT_TYPE', 'application/json')

            if format.startswith('multipart'):
                data = request.POST.copy()
                photo = Article()
                photo.thumbnail = request.FILES['thumbnail']
                photo.title = request.POST.get('title')
                # ... etc
                return data
            return super(ArticleResource, self).deserialize(request, data, format)

        # overriding the save method to prevent the object getting saved twice 
        def obj_create(self, bundle, request=None, **kwargs):
            pass