Answer the question
In order to leave comments, you need to log in
How to change the error output format when validating a field in a serializer?
There is a serializer
class ElementCommonInfoSerializer(serializers.ModelSerializer):
self_description = serializers.CharField(required=False, allow_null=True,
validators=[RegexValidator(regex=r'^[a-zA-Z0-9,.!? -/*()]*$',
message='The system detected that the data is not in English. '
'Please correct the error and try again.')]
)
....
class Meta:
model = Elements
fields = ('self_description',......)
{
"self_description": [
"The system detected that the data is not in English. Please correct the error and try again."
]
}
{
"general_errors": [
"The system detected that the data is not in English. Please correct the error and try again."
]
}
Answer the question
In order to leave comments, you need to log in
1. rewrite validate from the serializer.
2. in the is_valid() view and return errors in the response in the required form.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question