Handle non-string input for IP fields (#4338)

This commit is contained in:
Tom Christie 2016-08-01 16:14:26 +01:00 committed by GitHub
parent e997713313
commit aa349fe767
2 changed files with 5 additions and 1 deletions

View File

@ -804,7 +804,10 @@ class IPAddressField(CharField):
self.validators.extend(validators) self.validators.extend(validators)
def to_internal_value(self, data): def to_internal_value(self, data):
if data and ':' in data: if not isinstance(data, six.string_types):
self.fail('invalid', value=data)
if ':' in data:
try: try:
if self.protocol in ('both', 'ipv6'): if self.protocol in ('both', 'ipv6'):
return clean_ipv6_address(data, self.unpack_ipv4) return clean_ipv6_address(data, self.unpack_ipv4)

View File

@ -663,6 +663,7 @@ class TestIPAddressField(FieldValues):
'127.122.111.2231': ['Enter a valid IPv4 or IPv6 address.'], '127.122.111.2231': ['Enter a valid IPv4 or IPv6 address.'],
'2001:::9652': ['Enter a valid IPv4 or IPv6 address.'], '2001:::9652': ['Enter a valid IPv4 or IPv6 address.'],
'2001:0db8:85a3:0042:1000:8a2e:0370:73341': ['Enter a valid IPv4 or IPv6 address.'], '2001:0db8:85a3:0042:1000:8a2e:0370:73341': ['Enter a valid IPv4 or IPv6 address.'],
1000: ['Enter a valid IPv4 or IPv6 address.'],
} }
outputs = {} outputs = {}
field = serializers.IPAddressField() field = serializers.IPAddressField()