From 5a4ac4ead986387216e0c0e1b4d4b6a537f87e0b Mon Sep 17 00:00:00 2001 From: Josh Smeaton Date: Mon, 5 Jan 2015 21:32:25 +1100 Subject: [PATCH] Fixed #24078 -- Removed empty strings from GenericIPAddressField --- django/db/models/fields/__init__.py | 2 +- docs/releases/1.8.txt | 5 +++++ tests/model_fields/models.py | 4 ++++ tests/model_fields/tests.py | 18 +++++++++++++----- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index a1995452a3..e090dc625a 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1910,7 +1910,7 @@ class IPAddressField(Field): class GenericIPAddressField(Field): - empty_strings_allowed = True + empty_strings_allowed = False description = _("IP address") default_error_messages = {} diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 457850159f..1e0203bdc3 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -1009,6 +1009,11 @@ Miscellaneous different in some cases. There is no change to default values which are the result of a callable. +* ``GenericIPAddressField.empty_strings_allowed`` is now ``False``. Database + backends that interpet empty strings as null (only Oracle among the backends + that Django includes) will no longer convert null values back to an empty + string. This is consistent with other backends. + .. _deprecated-features-1.8: Features deprecated in 1.8 diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index 8927ae8830..376a59c413 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -176,6 +176,10 @@ class VerboseNameField(models.Model): field22 = models.URLField("verbose field22") +class GenericIPAddress(models.Model): + ip = models.GenericIPAddressField(null=True, protocol='ipv4') + + ############################################################################### # These models aren't used in any test, just here to ensure they validate # successfully. diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index d891645ca0..9ef46006e1 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -22,11 +22,11 @@ from django.utils import six from django.utils.functional import lazy from .models import ( - Foo, Bar, Whiz, BigD, BigS, BigIntegerModel, Post, NullBooleanModel, - BooleanModel, PrimaryKeyCharModel, DataModel, Document, RenamedField, - DateTimeModel, VerboseNameField, FksToBooleans, FkToChar, FloatModel, - SmallIntegerModel, IntegerModel, PositiveSmallIntegerModel, PositiveIntegerModel, - WhizIter, WhizIterEmpty) + Bar, BigD, BigIntegerModel, BigS, BooleanModel, DataModel, DateTimeModel, + Document, FksToBooleans, FkToChar, FloatModel, Foo, GenericIPAddress, + IntegerModel, NullBooleanModel, PositiveIntegerModel, PositiveSmallIntegerModel, + Post, PrimaryKeyCharModel, RenamedField, SmallIntegerModel, VerboseNameField, + Whiz, WhizIter, WhizIterEmpty) class BasicFieldTests(test.TestCase): @@ -688,6 +688,14 @@ class GenericIPAddressFieldTests(test.TestCase): form_field = model_field.formfield() self.assertRaises(ValidationError, form_field.clean, '127.0.0.1') + def test_null_value(self): + """ + Null values should be resolved to None in Python (#24078). + """ + GenericIPAddress.objects.create() + o = GenericIPAddress.objects.get() + self.assertIsNone(o.ip) + class PromiseTest(test.TestCase): def test_AutoField(self):