Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (45.3k points)

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?

1 Answer

0 votes
by (16.8k points)
edited by

You can easily try this in 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.",



To know more about this you can have a look at the following video:-

Related questions

Browse Categories