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 everything under "non_field_errors" which is what would happen if I were to raise a ValidationError in the object-level validate method. Below is an illustration of what I'm trying to achieve:
MySerializer(ModelSerializer):
...
def validate(self, data):
field_val1 = data['field_val1']
field_val2 = data['field_val2']
if not self._is_field_valid(field_val1, field_val2):
# The below line is how I would do what I want with Django
# Forms, however, it's not valid in DRF
self._errors['field_val1'] = 'this field is not valid'
The desired error response is:
{'field_val1': ['this field is not valid']}