Django: How to override unique_together error message?

Question:

In a model’s Meta class, I define a unique_together. I have a ModelForm based on this model. When I call is_valid on this ModelForm, an error will automatically raised if unique_together validation fails. That’s all good.

Now my problem is that I’m not satisfied with the default unique_together error message. I want to override it. How can I do that? For a field related error, I can easily do that by setting error_messages on the field parameters. But unique_together is a non field error. How can I override a non field error message?

Asked By: Georgie Porgie

||

Answers:

After a quick check, it seems that unique_together validation errors are hard-coded deep in django.db.models.Model.unique_error_message :

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)
        }

So maybe you should try to override this method from your model, to insert your own message !?

However, I haven’t tried, and it seems a rather brutal solution ! But if you don’t have something better, you might try…

Answered By: sebpiq

Notice: A lot had changed in Django since this answer. So better check other answers…

If what sebpiq is true( since i do not check source code), then there is one
possible solution you can do, but it is the hard way…

You can define a validation rule in your form, as it described here

You can see examples of validation with more than one field, so by using this method, you can define a unique together check before standard django unique check executed…

Or the worst one, you can do a validation in your view before you try to save the objects…

Answered By: FallenAngel

You might take a look at overriding django/db/models/base.py:Model._perform_unique_checks() in your model.

In that method you can get the “original” errors:

    errors = super(MyModel, self)._perform_unique_checks(unique_checks)

— then modify and return them upwards.

Answered By: Tomasz ZieliƄski

Update 2016/10/20: See jifeng-yin‘s even nicer answer below for Django >= 1.7

The nicest way to override these error messages might be to override the unique_error_message method on your model. Django calls this method to get the error message whenever it encounters a uniqueness issue during validation.

You can just handle the specific case you want and let all other cases be handled by Django as usual:

def unique_error_message(self, model_class, unique_check):
    if model_class == type(self) and unique_check == ('field1', 'field2'):
        return 'My custom error message'
    else:
        return super(Project, self).unique_error_message(model_class, unique_check)
Answered By: Sam Bull

You can do this since Django 1.7

from django.forms import ModelForm
from django.core.exceptions import NON_FIELD_ERRORS

class ArticleForm(ModelForm):
    class Meta:
        error_messages = {
            NON_FIELD_ERRORS: {
                'unique_together': "%(model_name)s's %(field_labels)s are not unique.",
            }
        }
Answered By: jifeng.yin

For DRF serializers you can use this

from rest_framework import serializers


class SomeSerializer(serializers.ModelSerializer):


    class Meta:
        model = Some
        validators = [
            serializers.UniqueTogetherValidator(
                queryset=model.objects.all(),
                fields=('field1', 'field2'),
                message="Some custom message."
            )
        ]

Here is the original source.

Answered By: Vaghinak
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.