Pickup custom message for URLField

This commit is contained in:
Carlton Gibson 2017-11-15 11:18:32 +01:00
parent e7dd68e8fa
commit c3fd86d323
2 changed files with 7 additions and 2 deletions

View File

@ -156,6 +156,9 @@ def get_field_kwargs(field_name, model_field):
# URLField does not need to include the URLValidator argument,
# as it is explicitly added in.
if isinstance(model_field, models.URLField):
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 not isinstance(validator, validators.URLValidator)

View File

@ -3,7 +3,7 @@ import datetime
import pytest
from django.core.validators import (
MaxLengthValidator, MaxValueValidator, MinLengthValidator,
MinValueValidator, URLValidator
MinValueValidator
)
from django.db import DataError, models
from django.test import TestCase
@ -597,7 +597,9 @@ class ValidatorMessageTests(TestCase):
def test_url_validator_message_is_copied_from_model(self):
class BlogModel(models.Model):
url = models.URLField(validators=[URLValidator(message='This URL is not valid.')])
url = models.URLField(
error_messages={"invalid": "This URL is not valid."}
)
class BlogSerializer(serializers.ModelSerializer):
class Meta: