Code

Ticket #5622: empty-ip-2.diff

File empty-ip-2.diff, 2.4 KB (added by jfunk, 4 years ago)

Version that does not break BooleanField

Line 
1Empty ipaddress raises an error (invalid input syntax for type inet: "")
2
3http://code.djangoproject.com/ticket/5622
4Index: Django-1.2.1/django/db/models/fields/__init__.py
5===================================================================
6--- Django-1.2.1.orig/django/db/models/fields/__init__.py
7+++ Django-1.2.1/django/db/models/fields/__init__.py
8@@ -898,6 +898,11 @@ class IPAddressField(Field):
9         kwargs['max_length'] = 15
10         Field.__init__(self, *args, **kwargs)
11 
12+    def get_db_prep_value(self, value, connection, prepared=False):
13+        if not prepared:
14+            value = self.get_prep_value(value)
15+        return value or None
16+
17     def get_internal_type(self):
18         return "IPAddressField"
19 
20Index: Django-1.2.1/tests/regressiontests/model_fields/models.py
21===================================================================
22--- Django-1.2.1.orig/tests/regressiontests/model_fields/models.py
23+++ Django-1.2.1/tests/regressiontests/model_fields/models.py
24@@ -66,6 +66,9 @@ class BooleanModel(models.Model):
25     bfield = models.BooleanField()
26     string = models.CharField(max_length=10, default='abc')
27 
28+class IP(models.Model):
29+    ip = models.IPAddressField(blank=True, null=True)
30+
31 ###############################################################################
32 # ImageField
33 
34Index: Django-1.2.1/tests/regressiontests/model_fields/tests.py
35===================================================================
36--- Django-1.2.1.orig/tests/regressiontests/model_fields/tests.py
37+++ Django-1.2.1/tests/regressiontests/model_fields/tests.py
38@@ -7,7 +7,7 @@ from django import forms
39 from django.db import models
40 from django.core.exceptions import ValidationError
41 
42-from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel
43+from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, IP
44 
45 # If PIL available, do these tests.
46 if Image:
47@@ -214,6 +214,14 @@ class SlugFieldTests(django.test.TestCas
48         bs = BigS.objects.get(pk=bs.pk)
49         self.assertEqual(bs.s, 'slug'*50)
50 
51+class IPFieldTests(django.test.TestCase):
52+    def test_ip_empty(self):
53+        """
54+        Check it's possible to use empty values in IP field (#5622).
55+        """
56+        ip = IP.objects.create(ip="")
57+        ip = IP.objects.get(pk=ip.pk)
58+        self.assertEqual(ip.ip, None)
59 
60 class ValidationTest(django.test.TestCase):
61     def test_charfield_raises_error_on_empty_string(self):