error_messages
?Error messages defined at the form field
level or at the form Meta level always take precedence over the error messages defined at the model field
level.
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.
# 您可以通过将NON_FIELD_ERRORS键添加到ModelForm的内部Meta类的error_messages字典中来覆盖模型验证所引起的NON_FIELD_ERRORS错误消息:
You can override the error messages from NON_FIELD_ERRORS
raised by model validation by adding the NON_FIELD_ERRORS
key to the error_messages
dictionary of the ModelForm
’s inner Meta
class:
from django.core.exceptions import NON_FIELD_ERRORS
from django.forms import ModelForm
class ArticleForm(ModelForm):
class Meta:
error_messages = {
NON_FIELD_ERRORS: {
‘unique_together‘: "%(model_name)s‘s %(field_labels)s are not unique.",
}
}
Django 自定义 error_messages={} 返回错误信息
原文:https://www.cnblogs.com/SunshineKimi/p/13886429.html