当重复条目尝试保存时它们应该是唯一的,即unique=True
,我想更改默认错误消息。就像这样:显示唯一字段的Django错误消息
email = models.EmailField(unique=True, error_messages={'unique':"This email has already been registered."})
但是,unique
在上述情况下是一种猜测,并不起作用。我也不知道错误的名称实际上是什么。有谁知道正确的名字?
请注意,此验证是模型级别,而不是表单验证。
编辑:更多 有点信息,此刻被form.errors
显示当前的错误消息:
[model_name] with this [field_label] already exists
这是不是很方便,所以我想重写它...
'unique'是现场选项:http://docs.djangoproject.com/en/1.3/ref/models/fields/#unique – Rob 2011-04-01 14:34:34
在你的标题你在谈论的IntegrityError,当尝试保存具有不唯一值的实例时引发这个问题,请参阅:http://docs.djangoproject.com/en/dev/ref/models/fields/#unique – Bjorn 2011-04-01 14:36:26
@Bjorn,也许我的标题有点混乱。我修改了它。我想重写标准错误消息,但我不知道错误消息的名称。我认为它被称为“独特”,但也许不是。也许我不能用这种方式重写它? – 2011-04-01 14:46:04