I use ModelForm to create my form. All works fine except 1 thing - validating the unique field. Code:
class Article(models.Model):
...
title = models.CharField(max_length=255, unique=True, error_messages={ max_length : max translation ,
unique : unique translation ,
required : req translation ,})
...
class ArticleForm(ModelForm):
...
title = forms.CharField(max_length=255, min_length=3, error_messages={ required : req translation ,
min_length : min translation ,
max_length : max translation ,
unique : unique translation ,})
但是,当我用非贵族头衔挽救我的形式时,我拿不出我的习俗错误,但我却出现违约错误。 如何纠正这一错误,我独特的实地错误是如何展示的?
EDIT: I found, I think, very convenient way to do that. Maybe someone will use it:)
def unique_error_message(self, model_class, unique_check):
if put_field_name_here in unique_check and len(unique_check) == 1:
return Here goes a custom unique error
return super(Article, self).unique_error_message(model_class, unique_check)