From 823bac36db2e4679d03fdc36e318da779767cac6 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 13 Jun 2011 20:15:13 +0000 Subject: [PATCH] Fixed #16246 -- Fixed GenericIPAddressField tests on PostgreSQL. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16395 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/__init__.py | 7 +++++++ django/forms/fields.py | 4 ++-- tests/modeltests/validation/models.py | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 94a9b65368..91ecbf2e7e 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -910,6 +910,7 @@ class BigIntegerField(IntegerField): empty_strings_allowed = False description = _("Big (8 byte) integer") MAX_BIGINT = 9223372036854775807 + def get_internal_type(self): return "BigIntegerField" @@ -922,6 +923,7 @@ class BigIntegerField(IntegerField): class IPAddressField(Field): empty_strings_allowed = False description = _("IPv4 address") + def __init__(self, *args, **kwargs): kwargs['max_length'] = 15 Field.__init__(self, *args, **kwargs) @@ -956,6 +958,11 @@ class GenericIPAddressField(Field): self.unpack_ipv4, self.error_messages['invalid']) return value + def get_db_prep_value(self, value, connection, prepared=False): + if not prepared: + value = self.get_prep_value(value) + return value or None + def get_prep_value(self, value): if value and ':' in value: try: diff --git a/django/forms/fields.py b/django/forms/fields.py index e3b4a44fc9..7bee36ee6e 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -965,8 +965,8 @@ class GenericIPAddressField(CharField): super(GenericIPAddressField, self).__init__(*args, **kwargs) def to_python(self, value): - if not value: - return '' + if value in validators.EMPTY_VALUES: + return u'' if value and ':' in value: return clean_ipv6_address(value, self.unpack_ipv4, self.error_messages['invalid']) diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py index da9e42a7fe..2d160ed4f1 100644 --- a/tests/modeltests/validation/models.py +++ b/tests/modeltests/validation/models.py @@ -84,9 +84,9 @@ class UniqueErrorsModel(models.Model): number = models.IntegerField(unique=True, error_messages={'unique': u'Custom unique number message.'}) class GenericIPAddressTestModel(models.Model): - generic_ip = models.GenericIPAddressField(blank=True, unique=True) - v4_ip = models.GenericIPAddressField(blank=True, protocol="ipv4") - v6_ip = models.GenericIPAddressField(blank=True, protocol="ipv6") + generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True) + v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4") + v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6") class GenericIPAddrUnpackUniqueTest(models.Model): generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True)