首页 > 其他 > 详细

Django 自定义 error_messages={} 返回错误信息

时间:2020-10-27 20:33:14      阅读:72      评论:0      收藏:0      [点我收藏+]

Considerations regarding model’s 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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!