Returning labels within validation errors in Django rest framework

When there are some validation errors on a request, DRF returns a dict object containing all the errors, something like this:

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

Is there anyway to change this behavior and make it to automatically return field names in each error? something like this:

{
    "first_name": [
        "First name field may not be blank."  # <<< The label of field: first_name
    ],
    "last_name": [
        "Last name field may not be blank."
    ],
    "email": [
        "Email field may not be blank."
    ]
}

or even a list:

[
    "First name field may not be blank.",  # <<< The label of field: first_name
    "Last name field may not be blank.",
    "Email field may not be blank.",
]

Note that I have more than of 80 end-points and serializers; I can't re-define all fields that are being automatically generated by ModelSerializers to add error_messages parameter.

  first_name = serializers.CharField(
        write_only=True,
        min_length=5,
        error_messages={
            "blank": "First name field cannot be empty.",
            "min_length": "First name field is too short.",
        },
    )
Back to Top