Georgie Porgie Georgie Porgie - 1 month ago 34
Python Question

Django: How to override unique_together error message?

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?

Answer

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...