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

Time:09-21

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

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

CodePudding user response:

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.
  ],
)
  • Related