Search code examples
djangodjango-modelsdjango-formsdjango-rest-frameworkdjango-validation

Django rest framework: How to change error response format globally


All the error messages return from django default validators ends with a dot (.). Is there anyway to remove that last dot from all messages globally.

Or, If someone help me to find a way to catch these error returning function, I can trim the last dot if exists in that section.

Sample error messages.

{
  "email": [
    "This field may not be blank."
  ]
}


{
  "email": [
    "Enter a valid email address."
  ]
}

Solution

  • If you want to change all of them, you should override the field's default messages somewhere in the application initialization:

    serializers.CharField.default_error_messages['blank'] = 'This field may not be blank'
    

    For example, the blank message can be found here