While you’re able to customize the error message for a unique field when defining your model…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Context(models.Model): | |
name = models.CharField(error_messages={'unique': u'My custom message'}) |
…you can’t do the same for a unique_together constraint. There’s an open ticket but it hasn’t seen much attention. In the meantime, you can override the unique_error_message in your model to provide a custom error message:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unique_error_message(self, model_class, unique_check): | |
if model_class == type(self) and unique_check == ('field1', 'field2'): | |
return 'Your custom error message.' | |
else: | |
return super(YourModel, self).unique_error_message(model_class, unique_check) |