In Django documentation https://docs.djangoproject.com/en/dev/ref/forms/validation/#raising-validationerror said that it is good practice to prodive error code while raising ValidationError exception.
# Good
ValidationError(_('Invalid value'), code='invalid')
# Bad
ValidationError(_('Invalid value'))
I have API in my application and I'm using form to validate input data.
If form is not valid, I whant to get these error codes (not error messages).
So I looked at source code of _clean_fields method of BaseForm:
https://github.com/django/django/blob/master/django/forms/forms.py#L278
except ValidationError as e:
self._errors[name] = self.error_class(e.messages)
if name in self.cleaned_data:
del self.cleaned_data[name]
As I understand this parameter (self.code) is not passed anywhere and can not be obtained after the form validation.
Can someone explain what the purpose of using this error code?