[Answered ]-Is there another way to change the error message on a ModelForm for a validate_unique error?

2👍

from django.utils.text import capfirst

class YourModel(models.Model):

    # fields

    def unique_error_message(self, model_class, unique_check):
        opts = model_class._meta
        model_name = capfirst(opts.verbose_name)

        # A unique field
        field_name = self._meta.unique_together[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)
        }
👤Thomas

Leave a comment