admin管理员组文章数量:1344617
I do not understand why in Django Rest Framework, my serializer do not serialize the file I gave it
I do a request like this in my Vue.js file:
const formData = new FormData();
formData.append("file", file.value);
formData.append("amount_pages", "" + 12);
try {
const response = await fetch(BACKEND_URL, {
method: "POST",
body: formData,
});
} catch (e: any) {
console.error(e);
}
On a view like that in my Django/DRF app:
from rest_framework import generics, serializers
class MySerializer(serializers.Serializer):
file = serializers.FileField(required=True)
amount_pages = serializers.IntegerField()
class Meta:
fields = [
"file",
"amount_pages",
]
class MyView(generics.CreateAPIView):
def post(self, request, *args, **kwargs):
serializer = MySerializer(data=request.data)
print(request.data) # <QueryDict: {'file': [<TemporaryUploadedFile: part1.pdf (application/pdf)>], 'amount_pages': ['12']}>
print(serializer.data) # {'file': None, 'amount_pages': 12}
I have already took a look at other issues but have not found any answers.
本文标签:
版权声明:本文标题:javascript - I do not understand why in Django Rest Framework, my serializer do not serialize the file I gave it - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743795991a2540440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论