快速檢查後,似乎unique_together
驗證錯誤是硬編碼在django.db.models.Model.unique_error_message
深:
def unique_error_message(self, model_class, unique_check):
opts = model_class._meta
model_name = capfirst(opts.verbose_name)
# A unique field
if len(unique_check) == 1:
field_name = unique_check[0]
field_label = capfirst(opts.get_field(field_name).verbose_name)
# Insert the error into the error dict, very sneaky
return _(u"%(model_name)s with this %(field_label)s already exists.") % {
'model_name': unicode(model_name),
'field_label': unicode(field_label)
}
# unique_together
else:
field_labels = map(lambda f: capfirst(opts.get_field(f).verbose_name), unique_check)
field_labels = get_text_list(field_labels, _('and'))
return _(u"%(model_name)s with this %(field_label)s already exists.") % {
'model_name': unicode(model_name),
'field_label': unicode(field_labels)
}
因此,也許你應該嘗試從模型覆蓋此方法,插入自己的消息!?
但是,我沒有嘗試過,它似乎是一個相當殘酷的解決方案!但如果你沒有更好的東西,你可以嘗試...
我想你最好標記另一個答案接受而不是我的,因爲Django 1.7做出了重要的改變,這也影響了我的答案。 – FallenAngel 2017-07-21 08:10:34