In Django Model i have the following
age = models.PositiveIntegerField(
_("age"),
error_messages={
'required':_('DRY ...'),
},)
however, the error message for required validator displays "This field is required"
But when I override the same in Form Meta class, it displays the correct required validator message.
class Meta:
model = model_name
fields = [
"age",
]
error_messages = {
'age': {
'required': _("This is not DRY."),
},
}
Do i really need to override the Form Meta, after declaring the error_messages on fields in Model?
Thanks
Two different messages being used at two different times.
Those attributes serve two different purposes.
From the docs for Field.error_messages
:
These error messages often don’t propagate to forms. See Considerations regarding model’s error_messages.
Then, from that referenced link:
Error messages defined on model fields
are only used when the ValidationError
is raised during the model validation step and no corresponding error messages are defined at the form level.
which then ends up leading you to the docs for Validation on a ModelForm which shows that the form validation is performed before the model validation.
Forms don’t necessarily perform precisely the same validation as a model. It is possible that a value submitted to a form passes the form validation but not the model validation - and vice versa. It’s up to you to ensure you understand what validation is being performed at each step, and manage the messages accordingly.
I have read the documentation a number of times, and still doing to understand better, how forms are rendered. Thanks