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.
dir(e)and see the output. It will show you the available methods and variables on that object['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', '__weakref__', 'args', 'error_dict', 'message_dict', 'messages', 'update_error_dict', 'with_traceback']. Not sure what to do with it.error_dictormessage_dictormessageswould be the attributes to try with. Just try e.messages