2
votes

When I am calling is_valid on my serializer, some of the data passed to the serializer is not getting saved. The files field is available in the serializer.initial_data, but does not get saved in serializer.validated_data. Any ideas?

Serializer:

class SomeSerializer(serializers.Serializer):
    email = serializers.EmailField()
    files = serializers.ListField(
        child=serializers.FileField()
        )

And the following view:

class SomeView(mixins.CreateModelMixin, generics.GenericAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = SomeSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        email = serializer.validated_data["email"]
        files = serializer.validated_data.get("files")
        #Do something here
        return response
Without seeing the full code, this could happen if you have any fields marked as read_only in SomeSerializer. - themanatuf
The full serializer code just has two more fields(both CharFields). No field in the serializer is marked read_only - Ankur Gupta
I haven't used a FileField serializer before, so I'm not sure what else to suggest other than DRF thinks the list contents of files aren't really files. - themanatuf
@themanatuf Thanks. That actually gave me ideas. - Ankur Gupta
It turns out that I was POST'ing data in an incorrect form to the serializer. While the list elements should have be sent in the data[files[index]] format in this case, I was sending it as data[files] = [<InMemoryObject1>,<InMemoryObject2>,<InMemoryObject3> ] - Ankur Gupta