I do not understand why in Django Rest Framework, my serializer do not serialize the file I gave it
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.