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
The . is_valid() method takes an optional raise_exception flag that will cause it to raise a serializers. ValidationError exception if there are validation errors.
We can validate the serializer by calling the method " is_valid() ". It will return the boolean(True/False) value. If the serializer is not valid then we can get errors by using the attribute "errors".
Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
The ModelSerializer class is the same as a regular Serializer class, except that: It will automatically generate a set of fields for you, based on the model. It will automatically generate validators for the serializer, such as unique_together validators. It includes simple default implementations of .
Just use below code to find out validation errors:
print(serializer.errors)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With