Hi DRFers,
I've got two simple serializers:
class ImportFileSerializer(serializers.Serializer):
file_to_import = serializers.FileField()
class ImportFilesSerializer(serializers.Serializer):
files = serializers.ListField(child=ImportFileSerializer())
I can test the single-file upload pretty easily:
def test_import_single_file_valid_everything(self) -> None:
filename="foo.csv"
client = APIClient()
url = hosts_reverse("import_file-list")
client.login(username=self.user.username, password="xxxxxxxxx")
with open(f"{filename}", "rb") as csvfile:
# patch
response = client.post(
url,
{
"file_to_import": csvfile,
"file_name": filename,
},
format="multipart",
)
self.assertEqual(response, success_response)
And my single-file upload view loves this - it does a `ImportFileSerializer(data=request.data)` and an is_valid() check and I can get the file, etc.
BUT. My multiple file test doesn't work (the other code is the same as the above test):
file_names = [ "foo1", "foo2", "foo3" ]
post_body = {}
post_body["files"] = []
for filename in file_names:
with open(f"{filename}", "rb") as csvfile:
post_body["files"].append({
"file_to_import": csvfile,
"file_name": filename,
})
response = client.post(
url, post_body,
format="multipart",
)
My ImportFilesSerializer passes the is_valid() check but returns an empty list for the validated_data. Any idea why?
Thanks!
-Adam