creyD
creyD

Reputation: 2126

Field data disappears in Django REST framework

I have a field "plugins" (see below) in my serializer and this is a serializer which also contains a file upload which is why the MultiPartParser is used. My view is pretty much standard, and the plugins field data also shows up in the request.data, however it doesn't show up in the validated_data of the serializer. To bring a minimalistic example, this would be my serializer:

class CreationSerializer(serializers.ModelSerializer, FileUploadSerializer):
    plugins = serializers.ListSerializer(
        child=serializers.CharField(), required=False, write_only=True)

    class Meta:
        fields = ['plugins'] + FileUploadSerializer.Meta.fields
        model = Company

    def create(self, validated_data):
        print(validated_data)

While this would be my views.py:

@swagger_auto_schema(request_body=CreationSerializer(), responses={201: CreationSerializer()}, operation_id='the_post')
def create(self, request, *args, **kwargs):
    print(request.data)
    return super().create(request, *args, **kwargs) # which uses mixins.CreateModelMixin

I tried adding another parser (i.e. JSONParser) to the parsers list, but this doesn't change anything.

Upvotes: 1

Views: 331

Answers (1)

Felix Eklöf
Felix Eklöf

Reputation: 3720

Does it work if you replace with this? I'm not sure but maybe drf doesn't recognize ListSerializer as a field, I've always used a Serializer with many=True:

plugins = serializers.ListField(child=serializers.CharField(), required=False, write_only=True)

Upvotes: 1

Related Questions