title: How Configure Django Rest Framework to return errors with custom text
body: <p>I want to customize the JSON response when adding a new item to the database it returns the following. </p>
  
  <pre><code>HTTP 400 BAD REQUEST 
  Content-Type: application/json Vary: 
  Accept Allow: POST, OPTIONS
  
  {
  "nick": [
      "Users with this Nick already exists."
      ]
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>{
  "nick": [
      "Your username is empty"
      ]
  }
  </code></pre>
  
  <p>I want it to return (This username already exists, please use a different one.)</p>
  
  <p>or  </p>
  
  <pre><code>"Username %s already exists", (self.nick)
  </code></pre>
  
  <p>I used the following sample but does not work if the value is empty or invalid.</p>
  
  <pre><code>def validate_title(self, attrs, source):
      """
      Check that the blog post is about Django.
      """
      value = attrs[source]
      if "django" not in value.lower():
          raise serializers.ValidationError("Blog post is not about Django")
      return attrs
  </code></pre>
  
  <p>this is the JSON that gets sent to the API.</p>
  
  <pre><code>{
      "name": "myname",
      "nick":"",
      "type_account":"1",
      "email":"my-email@gmail.com",
      "pass_field":"12345"
  }
  </code></pre>
  
  <p>serializers.py</p>
  
  <pre><code>class userSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = users
          fields = ('nick', 'email', 'pass_field', 'type_account')
  
      def validate_nick(self, attrs, source):
  
          value = attrs[source]
          if not value:
              raise serializers.ValidationError('Username cannot be empty')
          elif self.Meta.model.objects.filter(nick=value).exists():
              raise serializers.ValidationError("Username "+value+" is in use")
          return attrs
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>@api_view(['POST'])
  def user_add(request):
      """
      Saves a new user on the database
      """
  
      if request.method == 'POST':
  
          serializer = userSerializer(data=request.DATA)
          if serializer.is_valid():
              serializer.save()
              return Response(serializer.data, status=status.HTTP_201_CREATED)
          else:
              return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>