2

Django Rest Framework does it's own validations. For example when creating a user it can return something like this:

{
    "username": [
        "A user with that username already exists."
    ],
    "password": [
        "This field may not be blank."
    ],
    "name": [
        "This field may not be blank."
    ]
}

Is there an easy way to return an id error instead of a text message? And if it is possible where do I find the id errors meanings? For example, if id error 1 is A user with that username already exists. and id error 2 is This field may not be blank.:

{
    "username": [
        "1"
    ],
    "password": [
        "2"
    ],
    "name": [
        "2"
    ]
}

So then in the applications that uses this server can use their own texts (for example, handle different laguages).

I don't want to use my own validations or change the error message. I want to send the id of the error. It's something that DRF should have, but I don't know if it is possible.

Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
  • Possible duplicate of [Custom error messages in Django Rest Framework serializer](https://stackoverflow.com/questions/26943985/custom-error-messages-in-django-rest-framework-serializer) – Brown Bear Nov 12 '17 at 17:43
  • I don't want ot use my own validators. I am wondering if there is the possibility tu use drf validators but instead of returning a text message, return an id that I can use in client. – Damia Fuentes Nov 12 '17 at 18:36
  • read the part of `extra_kwargs` in the answer – Brown Bear Nov 13 '17 at 07:57
  • You don't understand my question. I suppose drf doesn't have what was looking for. I will have to do it field by field and defining my own ids. – Damia Fuentes Nov 13 '17 at 09:14
  • Actually you can try to define your own exception handling - you can read more about it here: http://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling If you can somehow and in generic way identify the errors - you can modify the response to your needs. – opalczynski Nov 13 '17 at 17:45

0 Answers0