lundi 4 septembre 2017

Model validation: can I access ValidationError code?

I have two directly related questions.

Django documentation recommends raising ValidationError with a code:

# Good
ValidationError(_('Invalid value'), code='invalid')

# Bad
ValidationError(_('Invalid value'))

How can I access this code in tests? All my attempts to use as_data, as_json, or simply .code on the caught exception fail. Unfortunately, the suggestions I see all relate to form validation. My test validates the models.

It is almost the same question as asked before (I don't use forms).

The related question: the same documentation page linked above gives a few examples of how to raise ValidationError, and while "Raising ValidationError" section recommends using the code, "Using validation in practice" never mentions it again, and the examples there don't use code. I wonder if that's an indication of this feature being stale.

Aucun commentaire:

Enregistrer un commentaire