9
votes

My class

class SprintSerializer(serializers.ModelSerializer):

    links = serializers.SerializerMethodField()

    class Meta:
        model = Sprint
        fields = ('id', 'name', 'description', 'end', 'links', )

In my shell,I populated a serializer with data

serializer = SprintSerializer(data=({'name':'JHolmes','description':'ambassador','end':'2019-01-27T15:17:10.375877'}))

Then

serializer.data
{'name': 'JHolmes', 'description': 'ambassador', 'end': '2019-01-27T15:17:10.375877'}
serializer.validated_data
{}
serializer.is_valid()
False

Why is an instance serializer False? EDIT As Berry pointed out,data format was wrong

serializer.errors
{'end': [ErrorDetail(string='Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]].', code='invalid')]}

Solved issue

'end':'2019-01-27'
serializer.is_valid()
True
2
and what in the serializer.errors?Brown Bear
yes,data format was wrong.Richard Rublev

2 Answers

10
votes

Just use below code to find out validation errors:

print(serializer.errors)
6
votes

I had a similar issue and as I saw in the previusly comments, after serializer.is_valid() line, you can write the next one code: 'print(serializer.errors)'. Which bring you information from the console about why your serialize model is failling. A way you could find the info could be:

if not serializer.is_valid():
   print(serializer.errors)