通过分段卷曲保存二进制文件

时间:2019-11-07 09:30:16

标签: django django-rest-framework

我想通过DRF发布请求将文件保存到二进制字段和其他一些详细信息中。下面是我的代码:

    class TestUpload(models.Model):
        id = models.AutoField(primary_key=True, editable=False)
        code = models.CharField(max_length=64)
        name = models.CharField(max_length=128)
        description = models.CharField(max_length=1024)
        data = models.BinaryField()

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

    def post(self, request, format=None):
        serializer = TestUploadSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
        return Response({'received data': request.data})

class TestUploadSerializer(serializers.ModelSerializer):
    class Meta:
        model = wm.TestUpload
        fields = [
            'code',
            'name',
            'description',
            'data',
        ]

下面是我的邮递员卷曲代码:

curl -X POST \
  http://localhost:80/testupload/ \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -H 'postman-token: 447d7078-faa5-37e9-30ae-5ade00e626e6' \
  -F code=code \
  -F name=name \
  -F 'description=Description' \
  -F data=@test.js

使用上述代码,数据库中的行已成功添加到数据库中,但data列为空,因此未插入文件。我想念什么?

0 个答案:

没有答案