Code

Ticket #5622: empty-ip.diff

File empty-ip.diff, 2.3 KB (added by stephaner, 4 years ago)

Updated to trunk (> Django 1.2.1)

Line 
1diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
2index 65b60a0..e639d0f 100644
3--- a/django/db/models/fields/__init__.py
4+++ b/django/db/models/fields/__init__.py
5@@ -269,7 +269,10 @@ class Field(object):
6         """
7         if not prepared:
8             value = self.get_prep_value(value)
9-        return value
10+        if self.empty_strings_allowed:
11+            return value
12+        else:
13+            return value or None
14 
15     def get_db_prep_save(self, value, connection):
16         "Returns field's value prepared for saving into a database."
17diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
18index 45cd223..035fcd2 100644
19--- a/tests/regressiontests/model_fields/models.py
20+++ b/tests/regressiontests/model_fields/models.py
21@@ -66,6 +66,9 @@ class BooleanModel(models.Model):
22     bfield = models.BooleanField()
23     string = models.CharField(max_length=10, default='abc')
24 
25+class IP(models.Model):
26+    ip = models.IPAddressField(blank=True, null=True)
27+
28 ###############################################################################
29 # ImageField
30 
31diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
32index 72a7d4d..7798622 100644
33--- a/tests/regressiontests/model_fields/tests.py
34+++ b/tests/regressiontests/model_fields/tests.py
35@@ -7,7 +7,7 @@ from django import forms
36 from django.db import models
37 from django.core.exceptions import ValidationError
38 
39-from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel
40+from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, IP
41 
42 # If PIL available, do these tests.
43 if Image:
44@@ -214,6 +214,14 @@ class SlugFieldTests(django.test.TestCase):
45         bs = BigS.objects.get(pk=bs.pk)
46         self.assertEqual(bs.s, 'slug'*50)
47 
48+class IPFieldTests(django.test.TestCase):
49+    def test_ip_empty(self):
50+        """
51+        Check it's possible to use empty values in IP field (#5622).
52+        """
53+        ip = IP.objects.create(ip="")
54+        ip = IP.objects.get(pk=ip.pk)
55+        self.assertEqual(ip.ip, None)
56 
57 class ValidationTest(django.test.TestCase):
58     def test_charfield_raises_error_on_empty_string(self):