I would like to change the default error message when duplicate entries try to save when they should be unique i.e. unique=True. Much like this:
email = models.EmailField(unique=True, error_messages={'unique':"This email has already been registered."})
But, unique in the above case was a guess, and doesn’t work. Neither can I find out what the name of the error actually is. Does anyone know the correct name?
Note, this validation is model level, not form validation.
EDIT:
A bit more info, at the moment the current error message is displayed by form.errors:
[model_name] with this [field_label] already exists
Which isn’t very user friendly, so I wanted to override it…
This error message is apparently hard-coded in the
django/db/models/base.pyfile.One way to solve this is to create your custom model derived from
EmailFieldand override theunique_error_messagemethod. But beware that it might break things when you upgrade to newer versions of Django.