From 4681d65048ca2553895e10c2c492997b0a78ffba Mon Sep 17 00:00:00 2001 From: Joshua Phillips Date: Fri, 29 Apr 2016 11:44:56 +0100 Subject: [PATCH] Fixed #26557 -- Converted empty strings to None when saving GenericIPAddressField. --- django/db/backends/base/operations.py | 2 +- docs/releases/1.9.6.txt | 3 +++ tests/model_fields/test_genericipaddressfield.py | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/db/backends/base/operations.py b/django/db/backends/base/operations.py index 6cd466411a0..fe21fd6e585 100644 --- a/django/db/backends/base/operations.py +++ b/django/db/backends/base/operations.py @@ -499,7 +499,7 @@ class BaseDatabaseOperations(object): Transforms a string representation of an IP address into the expected type for the backend driver. """ - return value + return value or None def year_lookup_bounds_for_date_field(self, value): """ diff --git a/docs/releases/1.9.6.txt b/docs/releases/1.9.6.txt index 7e3f6aed6a2..769d04945a1 100644 --- a/docs/releases/1.9.6.txt +++ b/docs/releases/1.9.6.txt @@ -24,3 +24,6 @@ Bugfixes * Fixed a regression causing the cached template loader to crash when using template names starting with a dash (:ticket:`26536`). + +* Restored conversion of an empty string to null when saving values of + ``GenericIPAddressField`` on SQLite and MySQL (:ticket:`26557`). diff --git a/tests/model_fields/test_genericipaddressfield.py b/tests/model_fields/test_genericipaddressfield.py index 7a3705ef185..c65fe805cb1 100644 --- a/tests/model_fields/test_genericipaddressfield.py +++ b/tests/model_fields/test_genericipaddressfield.py @@ -29,6 +29,14 @@ class GenericIPAddressFieldTests(TestCase): o = GenericIPAddress.objects.get() self.assertIsNone(o.ip) + def test_blank_string_saved_as_null(self): + o = GenericIPAddress.objects.create(ip='') + o.refresh_from_db() + self.assertIsNone(o.ip) + GenericIPAddress.objects.update(ip='') + o.refresh_from_db() + self.assertIsNone(o.ip) + def test_save_load(self): instance = GenericIPAddress.objects.create(ip='::1') loaded = GenericIPAddress.objects.get()