how to customize django model message for email

163 Views Asked by At

I created User model in Django as:

email = models.EmailField(
        verbose_name="Email Address", unique=True, null=True, blank=True)

When I try to register user with same email it show message user with this Email Address already exist, how can I customize this message to User with this email exist

2

There are 2 best solutions below

2
Navaneeth Sharma On

As per my understanding of the problem, below answer should solve the issue

models.EmailField(
    verbose_name="Email Address", unique=True, null=True, blank=True,
    error_messages={
        'unique': "User with this email already exists.",
    }
)
12
Sunderam Dubey On

You can use unique attribute of error messages dict and customize it according to your custom message (User with this email exist) so:

class User(models.Model):
    email = models.EmailField(
        verbose_name="Email Address", unique=True,
        error_messages={
            'unique': 'User with this email exists.',
        }
    )