django休息框架文件上传与嵌套可写序列化程序 [英] django rest framework file upload with nested writable serializers

查看:117
本文介绍了django休息框架文件上传与嵌套可写序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

$ b类ImageSerializer(序列化器.HyperlinkedModelSerializer):
annotations = AnnotationSerializer(many = True,required = False)

class Meta:
depth = 1
model = Image
exclude =('owner',)

注释具有图像外键属性,因此图像可能有多个注释。我想通过对图像端点的发布请求创建嵌套注释的图像,包括该图像的注释列表。发布我的数据json编码到图像端点确实工作,并创建一个具有适当注释的图像。



但是当我尝试上传一个实际的图像时,我必须使用多部分/ form编码的post请求,而不是一个json,以使fileupload成为可能。现在我很难得到这个请求中包含的图像注释的嵌套列表。也许我可以把一个json编码的字符串放在一些表单字段中,并在视图中手动解析它,覆盖request.DATA,但这似乎是非常难看的。



想知道是否有更好的方式来实现我想要做的事情:)

解决方案

你是什么意思请求中包含图像注释的嵌套列表的困难时间?当您发送 multpart / form-data 发布请求时,是否包含在request.data中的嵌套符号列表数据? (请使用 request.data 而不是 request.DATA request.FILES )。请使用一些调试工具,如 pdb 来检查您的 request.data



为了支持可写入的嵌套序列化程序,我想你应该覆盖 POST 方法的 create()函数,您可以从这里查看更多详情。对不起直接给出答案,我需要更多有关你的模型的细节。



如果你想发布 JSON 而不是 multipart / form-data ,您可以使用 base64 表示图(但是文件的大小将增长近33%)。


class AnnotationSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Annotation


class ImageSerializer(serializers.HyperlinkedModelSerializer):
    annotations = AnnotationSerializer(many=True, required=False)

    class Meta:
        depth = 1
        model = Image
        exclude = ('owner‘,)

An annotation has an image foreign key attribute and so images have potentially multiple annotations. I’d like to create an image with nested annotations via a post request to an images endpoint including the list of annotations for this image. Posting my data json encoded to the images endpoint does work and creates an image with appropriate annotations.

But when I try to upload an actual image, I have to use a multipart/form-encoded post request instead of a json one to make fileupload possible. And now I’m having a hard time getting my nested list of image annotations included in this request. Maybe I could put a json encoded string in some form-field and manually parse it in the view, overwriting request.DATA, but this seems to be really ugly.

I wonder whether there’s a better way to achieve what I’m trying to do :).

解决方案

What do you mean that having a hard time gettting the nested list of image annotations include in the request? When you send the multpart/form-data post request, is the nested notation list data included in the request.data? (Please use request.data instead of request.DATA and request.FILES). Please use some debug tools like pdb to check your request.data.

For supporting writable nested serializer, I think you should overrite the create() function for POST method, and you can find more details from here. Sorry for not giving the answer directly, I need more details about your models.

And if you want to post JSON instead of multipart/form-data, you can use base64 for representing the figure (But the size of the file will grow almost 33% larger).

这篇关于django休息框架文件上传与嵌套可写序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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