diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 333afc5..4e21791 100644
a
|
b
|
from django.core import checks, exceptions, validators
|
18 | 18 | # When the _meta object was formalized, this exception was moved to |
19 | 19 | # django.core.exceptions. It is retained here for backwards compatibility |
20 | 20 | # purposes. |
21 | | from django.core.exceptions import FieldDoesNotExist # NOQA |
| 21 | from django.core.exceptions import FieldDoesNotExist, ValidationError # NOQA |
22 | 22 | from django.db import connection, connections, router |
23 | 23 | from django.db.models.query_utils import QueryWrapper, RegisterLookupMixin |
24 | 24 | from django.utils import six, timezone |
… |
… |
class GenericIPAddressField(Field):
|
1994 | 1994 | if not isinstance(value, six.string_types): |
1995 | 1995 | value = force_text(value) |
1996 | 1996 | value = value.strip() |
| 1997 | if not self.unpack_ipv4 and ':' in value and '.' in value: |
| 1998 | raise ValidationError(_('Mixed IPv4 and IPv6 representation is not allowed when unpack_ipv4 is disabled'), |
| 1999 | code='invalid') |
1997 | 2000 | if ':' in value: |
1998 | 2001 | return clean_ipv6_address(value, |
1999 | 2002 | self.unpack_ipv4, self.error_messages['invalid']) |
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index 70f039e..dd99ecc 100644
a
|
b
|
class GenericIPAddressFieldTests(test.TestCase):
|
904 | 904 | with self.assertRaises(ValidationError): |
905 | 905 | form_field.clean('127.0.0.1') |
906 | 906 | |
| 907 | def test_genericipaddressfield_formfield_unpack_ipv4(self): |
| 908 | """ |
| 909 | Test that GenericIPAddressField will reject a mixed IPv4 and IPv6 IP adress if unpack_ipv4=False. |
| 910 | See #26378 |
| 911 | """ |
| 912 | unpack_ip_field = models.GenericIPAddressField(null=False, protocol='both', unpack_ipv4=True) |
| 913 | not_unpack_ip_field = models.GenericIPAddressField(protocol='both', unpack_ipv4=False) |
| 914 | unpack_ip_field.clean('::ffff:0.0.0.0', None) |
| 915 | with self.assertRaises(ValidationError): |
| 916 | not_unpack_ip_field.clean('::ffff:0.0.0.0', None) |
| 917 | with self.assertRaises(ValidationError): |
| 918 | not_unpack_ip_field.clean('::ffff:2.0.0.0', None) |
| 919 | |
907 | 920 | def test_null_value(self): |
908 | 921 | """ |
909 | 922 | Null values should be resolved to None in Python (#24078). |