Search code examples
djangodjango-formsdjango-1.6

How can I override the 'unique' error on a ModelForm field?


I'm trying to override the 'unique' error message for a Field in my ModelForm. I'm trying to follow the docs for Django 1.6, but I can't get it to work. Pretty simple stuff, I have:

models.py:

class EmailAddress(Model):
    """An email address."""
    address = EmailField(unique=True)

    def __unicode__(self):
        return self.address

views.py:

class EmailAddressForm(ModelForm):
    class Meta:
        model = EmailAddress
        fields = {'address'}
        error_messages = {
            'address': {
                u'unique': _('That address has already been added.'),
            }
        }

If I enter a duplicate email, I get:

emailaddress_form.errors = {'address': [u'Email address with this Address already exists.']}.`

I've dug around in the source code, and as far as I can tell I'm passing the error_messages dict correctly into where it'll be picked up by the django.forms.models.fields_for_model function called by the Metaclass. Hopefully I'm missing something obvious. Any suggestions?


Solution

  • try This :-

    Change address field to :-

    address = models.EmailField(unique=True, error_messages={'unique':"That address has already been added."})