I have a serializer that validates fields based on the values of other fields, In the error response I would like to show each field error as a field error as opposed to showing
In case you are using built-in validators in DRF (which are in-fact django core validators) you have to preprocess django ValidationError coming from validator with a function get_error_detail
drf is using for this purpose.
def _validate_min_value(self, data, key):
try:
MinValueValidator(Decimal('0.01'))(data.get(key))
except ValidationErrorDjango as exc:
raise ValidationError(
{key: get_error_detail(exc)}
)
Note that ValidationErrorDjango is a ValidationError from django.core.exceptions
, while ValidationError is a one from rest_framework.exceptions