Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django rest framework get validation error object in detail

[ErrorDetail(string='This field is required.', code='required')]

How get code from this object. I want separate error code for custom error response, i tried many solutions but i didn't get any working answer.

like image 899
Chetan Shelake Avatar asked Jun 17 '19 10:06

Chetan Shelake


1 Answers

I found myself wanting to read the string field of the Error.

So for an error like:

<Response status_code=400, "application/json"> 
{'field_name': [ErrorDetail(string='the error message', code='invalid')]}

To get the string (e.g. for testing), I've used:

self.assertEqual(str(response.data['field_name'][0]), 'the error message')
like image 78
msonsona Avatar answered Nov 01 '22 21:11

msonsona