9👍
This error message is apparently hard-coded in the django/db/models/base.py
file.
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)
}
One way to solve this is to create your custom model derived from EmailField
and override the unique_error_message
method. But beware that it might break things when you upgrade to newer versions of Django.
54👍
Thank you very much.
email = models.EmailField(unique=True, error_messages={'unique':"This email has already been registered."})
this worked very well now.
If you want to customise error_messages like invalided
, do it in forms.ModelForm
email = forms.EmailField(error_messages={'invalid': 'Your email address is incorrect'})
But unique
message should be customised in model
field, as ben mentioned
email = models.EmailField(unique=True, error_messages={'unique':"This email has already been registered."})
- [Django]-How to send email via Django?
- [Django]-Switching to PostgreSQL fails loading datadump
- [Django]-Non-global middleware in Django
4👍
Since Django 1.4 the exact example you supply actually works. Maybe they found your error report and just fixed it?
https://github.com/django/django/blob/1.4.20/django/db/models/base.py#L780
- [Django]-Django static page?
- [Django]-Troubleshooting Site Slowness on a Nginx + Gunicorn + Django Stack
- [Django]-Best Fabric scripts for Django
0👍
The unique error message is constructed by django.db.models.base.unique_error_message
(at least on Django 1.3).
- [Django]-What is the correct way of returning an empty queryset in a Django View?
- [Django]-How to set or get a cookie value in django
- [Django]-Django: save() vs update() to update the database?