如何在django-tastypie上传POST请求的文件? [英] How do you upload a file with a POST request on django-tastypie?

查看:99
本文介绍了如何在django-tastypie上传POST请求的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我目前对我的API做cURL POST请求,所以

I currently do cURL POST requests to my API like so

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"username":"theusername", "api_key":"anapikey", "video_title":"a title", "video_description":"the description"}' http://localhost:8000/api/v1/video/ 

现在我需要能够添加一个视频文件到上传。我一直在寻找使用Tastypie上传文件几个小时,而且我还没有得到一个坚实的回应。我需要添加Base64编码吗?如果是这样的话在上传POST请求后,如何处理该文件?只是正常请求.FILES动作?我不想将文件保存到数据库,只需获取文件的路径。

but now I need to be able to add a video file to the upload. I have been looking around for a few hours about uploading files with Tastypie and I have not come up with one solid response. Do I need to add the Base64 encode? If so how? How do I acces the file after I have uploaded it with a POST request? Just normal request.FILES actions? I am not looking to save the file to the database, just get the path to the file.

#Models.py
class Video(models.Model):
    video_uploader = models.ForeignKey(User)
    video_path = models.CharField(max_length=128)
    video_views = models.IntegerField(default=0)
    upload_date = models.DateTimeField(auto_now_add=True)
    video_description = models.CharField(max_length=860)
    video_title = models.SlugField()

我是彻底混淆了如何实现文件上传系统的Tastypie所以任何帮助将非常感谢。谢谢!

I am thoroughly confused on how to implement a file upload system for Tastypie so any help would be very appreciated. Thanks!

推荐答案

这里是通过 MultiPart code> django-tastypie 。

Here is way to upload file by MultiPart through django-tastypie.

Models.py

class Video(models.Model):
    video_uploader = models.ForeignKey(User)
    video = models.FileField(_('Video'), upload_to='path_to_folder/') # save file to server
    video_views = models.IntegerField(default=0)
    upload_date = models.DateTimeField(auto_now_add=True)
    video_description = models.CharField(max_length=860)
    video_title = models.SlugField()

.py

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 VideoResource(MultipartResource, ModelResource):
   """
   Inherit this Resource class to `MultipartResource` Class
   """
   # Assuming you know what to write here 
   ...

然后通过 CURL

curl -H "Authorization: ApiKey username:api_key" -F "video=/path_to_video/video.mp3" -F "video_title=video title" http://localhost:8000/api/v1/video/ -v

这篇关于如何在django-tastypie上传POST请求的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆