我想在Django Rest Framework中编写一个视图集,该视图集允许用户一起上传带有这些订单号的新图像列表。但是我不知道在DRF中使用哪个字段。希望你们能帮助我。
我的序列化器:
class ContentSerializer(ModelSerializer):
fileUpload = FileField(max_length=100000, allow_empty_file=False, use_url=False)
order_num = IntegerField(required=False)
class Meta:
model = Article
fields = [
'fileUpload',
'order_num',
]
class ArticleSerializer(ModelSerializer):
file_uploads = ListField(child=ContentSerializer(),write_only=True)
class Meta:
model = Article
fields = [
'file_uploads',
]
我的视图集:
class FeedEditAPIView(ModelViewSet):
'''
Use Form Data to PATCH
'''
queryset = Article.objects.all()
serializer_class = ArticleSerializer
lookup_field = 'id'
def edit_article(self, request, id):
print request.POST.getlist('file_uploads') // This return [u'{"file":{},"order_num":0}']
当我打印请求POST file_uploads时,它返回[u'{"file":{},"order_num":0}']
。但是我想返回[u'{"file":<files_object>,"order_num":0}']
而不是
答案 0 :(得分:1)
使用multipartparser
,它支持文件上传和request.data
class FeedEditAPIView(ModelViewSet):
'''
Use Form Data to PATCH
'''
parser_classes = (MultiPartParser,)
queryset = Article.objects.all()
serializer_class = ArticleSerializer
lookup_field = 'id'