Dit artikel dateert van 2010 en is enkel beschikbaar in het Engels. Er is ondertussen veel veranderd, maar het blijft online staan als een soort krantenknipsel. Hou dat in gedachten bij het lezen.

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)