當重複條目嘗試保存時它們應該是唯一的,即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