Django REST : Making a custom serializer return proper error message

105 views Asked by At

I am using the following serializer code:

class MediaSerializer(serializers.Serializer):
uuid = serializers.CharField(required=True)
number_list = serializers.CharField(required=False)
file= serializers.FileField(required=True, allow_empty_file=False)

class Meta:
    fields = ('uuid', 'number_list', 'file')

The issue is that the error dictionary returned when validation fails on any field does not contain the field name. So if I don't pass any argument, I just get an error like this:

{"error": ["This field is required.", "This field is required."]}

I cannot say which field was missing. When using a ModelSerializer, I get field names also in this dictionary, but it's missing in custom serializer. I must be missing something obvious here, but can't get to it.

0

There are 0 answers