Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to change the inbuild validator Messages in Django Models

When ever i give wrong format email-id it should throw some other error message rather than
Enter a valid email address

models.py

class Publisher(models.Model):
    email=models.EmailField(blank=True,null=True)

serializer.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class PublisherSerializer(serializers.ModelSerializer):
        class Meta:
            model = Publisher
            fields = '__all__'

>Solution :

models.EmailField has validators.validate_email as its default validator, and its default error message is "Enter a valid email address.".

You can’t easily override a field’s default_validators, so you’d need to use a regular CharField instead, with your own validator.

email = models.CharField(
  max_length=254,  # same as EmailField
  blank=True,
  null=True,
  validators=[
    EmailValidator(message="very bad email :("),  # Custom message here.
  ],
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading