How to localize Django duplicate key value violates unique constraint error message?

Problem description

I would want to provide clear localized error messages to client using Django.

I have the following unique field:

class Foo(models.Model)
    email = models.EmailField(max_length=255, unique=True, default=None, blank=True,
                              null=True, verbose_name=gettext_lazy('email address'), error_messages={'unique': gettext_lazy("Email address is already registered to another user.")})

When I try to save duplicate email address, I still get an error

psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "foo_email_key"

Question

Why is my error message not localized? How can be the desired behaviour obtained?

Back to Top