mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 20:10:10 +03:00
Pickup custom message for EmailField
Set via `error_messages`, since `validate_email` is already present on field.
This commit is contained in:
parent
8f7c91d3a7
commit
e7dd68e8fa
|
@ -164,6 +164,9 @@ def get_field_kwargs(field_name, model_field):
|
|||
# EmailField does not need to include the validate_email argument,
|
||||
# as it is explicitly added in.
|
||||
if isinstance(model_field, models.EmailField):
|
||||
custom_message = model_field.error_messages.get("invalid", None)
|
||||
if custom_message is not None:
|
||||
kwargs.setdefault('error_messages', {}).update(invalid=custom_message)
|
||||
validator_kwarg = [
|
||||
validator for validator in validator_kwarg
|
||||
if validator is not validators.validate_email
|
||||
|
|
|
@ -2,7 +2,7 @@ import datetime
|
|||
|
||||
import pytest
|
||||
from django.core.validators import (
|
||||
EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
|
||||
MaxLengthValidator, MaxValueValidator, MinLengthValidator,
|
||||
MinValueValidator, URLValidator
|
||||
)
|
||||
from django.db import DataError, models
|
||||
|
@ -612,7 +612,9 @@ class ValidatorMessageTests(TestCase):
|
|||
|
||||
def test_email_validator_message_is_copied_from_model(self):
|
||||
class UserModel(models.Model):
|
||||
email = models.EmailField(validators=[EmailValidator(message='Please enter a valid email.')])
|
||||
email = models.EmailField(
|
||||
error_messages={"invalid": "Please enter a valid email."}
|
||||
)
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
|
|
Loading…
Reference in New Issue
Block a user