如何使用Django Rest APi上传文件

时间:2014-06-25 12:45:16

标签: django rest django-rest-framework

有人可以帮我在django rest api中用POST方法上传文件, 例如,当我启动时

curl -X POST 127.0.0.1:8000/api/v1/assets/ -d '{"name" = "my image   ","source"="/root/images/my_image2.jpg"}' -H "Content-Type: application/json"

我想上传my_image2.jpg

serializers.py:

from django.forms import widgets
from rest_framework import serializers
from .models import Asset

class AssetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Asset

views.py:

from .serializers import AssetSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from rest_framework import status
from rest_framework.decorators import api_view


class AssetAdd(APIView):


    def post(self, request, format=None):
        serializer = AssetSerializer(data=request.DATA)
        print serializer.data

        if serializer.is_valid():

            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

models.py

class Asset(models.Model):

    key = models.CharField(max_length=8, unique=True, editable=False)
    name = models.CharField(_('name'), max_length=200)
    source = models.FileField(_('file'), upload_to=upload_to, storage=default_storage)

    ext = models.CharField(max_length=15, editable=False)
    type = models.PositiveIntegerField(choices=ASSET_TYPE, max_length=15, editable=False)
    size = models.PositiveIntegerField(max_length=32, default=0, editable=False)

    _file_meta = models.TextField(editable=False, null=True, blank=True)

    public = models.BooleanField(default=False)
    position = models.PositiveIntegerField(default=1)

    object_id = models.PositiveIntegerField(default=1)
    content_type = models.ForeignKey(ContentType, blank=True, null=True)
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now_add=True, auto_now=True)

我是Django REST API中的新手,我阅读了文档http://www.django-rest-framework.org/api-guide/parsers.html#fileuploadparser,但仍然没有找到如何做到这一点

2 个答案:

答案 0 :(得分:21)

首先,您需要在视图中定义解析器。这是因为API需要知道要查找哪些标头。浏览器将文件作为表单数据传输,因此您需要同时使用MultiPartParser和FormParser。您也可以使用FileUploadParser,但需要确保客户端正在发送正确的HTTP标头。

from rest_framework.parsers import MultiPartParser, FormParser

class AssetAdd(APIView):
    parser_classes = (MultiPartParser, FormParser,)

然后,在post方法中,您的文件将出现在FILES QueryDict中:

def post(self, request, format=None):
    my_file = request.FILES['file_field_name']
    filename = '/tmp/myfile'
    with open(filename, 'wb+') as temp_file:
        for chunk in my_file.chunks():
            temp_file.write(chunk)

    my_saved_file = open(filename) #there you go

答案 1 :(得分:0)

这样编写模型:

from django.db import models
class PicModel(models.Model):
    file_to_be_uploaded = models.FileField(upload_to='/directory/where/you/what/to/upload/to')

如果您上传图像,则使用ImageField,但要按pip install pillow安装枕头。

在您serializers.py中,假设您正在使用rest_framework

from rest_framework import serializers
from app_name.models import PicModel

class PicSerializer(serializers.ModelSerializer):
    class Meta:
        model = PicModel
        fields = '__all__' #or any way you may like to display you fields

在您views.py中,假设您要使用基于类的视图:

from app_name.models import PicModel
from app_name.serializers import PicModelSerializer
#other necessary imports

class PicList(APIView):
    def post(self, request, format=None):#or format can be json or api
        serializer = PicModelSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            ............more code

要在json数据中进行测试,请输入以下内容:

{
    "file_to_be_uploaded":"//path//to//file//on//local//machine"
}