使用ListField序列化多个InMemoryUploadedFile:Django REST Framework

时间:2019-04-25 08:50:06

标签: python django django-rest-framework django-serializer django-uploads

如何使用InMemoryUploadedFile序列化多个serializers.ListField()


代码段

#views.py
@api_view(['POST', 'GET'])
def create_post(request):
    if request.method == 'POST':
        altered_request_data = request.data.copy()
        in_memory_upload_files_list = [value for value in request.FILES.values()]
        altered_request_data['files'] = in_memory_upload_files_list
        serializer = PostSerializer(data=altered_request_data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(data=serializer.data, status=201)
    else:
        qs = Post.objects.all()
        serializer = PostSerializer(qs, many=True)
        return Response(serializer.data)

#serilizers.py
class PostSerializer(serializers.ModelSerializer):
    files = serializers.ListField(child=serializers.FileField(), write_only=True)

    class Meta:
        fields = '__all__'
        model = Post

当前响应

{
    "files": {
        "0": [
            "The submitted data was not a file. Check the encoding type on the form."
        ]
    }
}

2 个答案:

答案 0 :(得分:1)

问题出在这一行,

altered_request_data['files'] = in_memory_upload_files_list

此处 altered_request_data QueryDict对象,因此,如果我们为其分配任何内容,则会调用__setitem__()方法

In [6]: from django.http import QueryDict                                                                                                                                                                          

In [7]: qd = QueryDict('a=1&a=2&c=3',mutable=True)                                                                                                                                                                 

In [8]: qd                                                                                                                                                                                                         
Out[8]: <QueryDict: {'a': ['1', '2'], 'c': ['3']}>

In [9]: my_list = [i for i in range(10)]                                                                                                                                                                           

In [10]: my_list                                                                                                                                                                                                   
Out[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [11]: qd['foo']=my_list                                                                                                                                                                                         

In [12]: qd                                                                                                                                                                                                        
Out[12]: <QueryDict: {'a': ['1', '2'], 'c': ['3'], 'foo': [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]}>

看到了吗? qd['foo'] 成为 列表列表 。也就是说,分配给 QueryDict 的所有内容都将存储在list对象中。

那么,解决方案是什么?

QueryDict类具有方法QueryDict.setlist将完成工作

In [15]: qd__new                                                                                                                                                                                                   
Out[15]: 

In [16]: qd__new = QueryDict('a=1&a=2&c=3',mutable=True)                                                                                                                                                           

In [17]: qd__new                                                                                                                                                                                                   
Out[17]: <QueryDict: {'a': ['1', '2'], 'c': ['3']}>

In [18]: my_list                                                                                                                                                                                                   
Out[18]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [19]: qd__new.setlist('foo_new',my_list)                                                                                                                                                                        

In [20]: qd__new                                                                                                                                                                                                   
Out[20]: <QueryDict: {'a': ['1', '2'], 'c': ['3'], 'foo_new': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}>

代码段

views.py

@api_view(['POST', 'GET'])
def create_post(request):
    if request.method == 'POST':
        altered_request_data = request.data.copy()
        in_memory_upload_files_list = [value for value in request.FILES.dict().values()]

        altered_request_data.setlist('files', in_memory_upload_files_list)

        serializer = PostSerializer(data=altered_request_data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(data=serializer.data, status=201)
    else:
        qs = Post.objects.all()
        serializer = PostSerializer(qs, many=True)
        return Response(serializer.data)

答案 1 :(得分:0)

您应该使用getlist的{​​{1}}方法,该方法将为您提供文件列表。

FILES

此处in_memory_upload_files_list = request.FILES.getlist('<file field name>') altered_request_data['files'] = in_memory_upload_files_list serializer = PostSerializer(data=altered_request_data) 将是您在其上上传文件的名称文件字段。

https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.QueryDict.getlist