→ Nederlandstalig? E: kevin@roam.beT: +32475435169

Django 1.2 upgrade: unique_error_message

While upgrading a project from Django 1.1 to Django 1.2 everything went flawless. Well, expect this one thing...

In Django 1.1 you could customize the error messages for unique constraints over multiple fields by overriding unique_error_message in your form. In Django 1.2 much of the validation has been moved to the model, including the unique_error_message checks.

Using Django 1.1 you'd use following code in your form class:

def unique_error_message(self, field):
    if field == ('field1', 'field2'):
        return "You should not do that"
    return super(MyForm, self).unique_error_message(field)

In Django 1.2 you'll have to change this to the following code in your model class:

def unique_error_message(self, model_class, unique_check):
    if unique_check == ('field1', 'field2'):
        return "You should not do that"
    return super(MyModel, self).unique_error_message(model_class, unique_check)

Are you a Java developer getting started with Django? Then you'll surely love my upcoming Django for Java Developers ebook! Find out how to manage dependencies, start, build and deploy a Django project from the perspective of a Java web developer.