I have a following problem.
I have a model level validation which checks data consistency on each save.
In serializers, if this model level validation works, it produces server error 500
with trace-back, whereas serializer.Validationerror
in serializer produces nice and clean 400 error
with error message in json.
In order to convert model level Validationerror
to serializers. Validationerror
I use following code in my serializers.
def perform_create(self, validated_data):
try:
return super().perform_create(validated_data)
except exceptions.ValidationError as err:
raise serializers.ValidationError(
f'Model level validation assertion -- {str(err)}'
) from err
It works, but I cant and don’t want to override each and every one serializer to convert Validationerror
to serializers. Validationerror
.
Question is - is it any way to catch all Validationerror and convert them to serializers. Validationerrors?