1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] In DRF, How to inject full `ErrorDetail` in the response, using a custom exception...

Discussão em 'Python' iniciado por Stack, Outubro 7, 2024 às 01:52.

  1. Stack

    Stack Membro Participativo

    I'm using a pretty complex custom handler in DRF. For example, for a given response, response.data could look like to:

    {'global_error': None, 'non_field_errors': [], 'field_errors': {'important_field': [ErrorDetail(string='Ce champ est obligatoire.', code='required')]}}


    However, when getting the actual response from the API, the ErrorDetail will be transformed in a simple string, losing the code information.

    Is there a simple way to ensure ErrorDetail is always written in a response as {"message": "...", "code": "..."} without transforming the response manually in the custom handler?

    I know there exists the DRF get_full_details() method that returns exactly this on an exception. But I'm the response level.

    Continue reading...

Compartilhe esta Página