快速检查后,似乎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