Ticket #5622: empty-ip-2.diff

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

Version that does not break BooleanField

  • django/db/models/fields/__init__.py

    Empty ipaddress raises an error (invalid input syntax for type inet: "")
    
    http://code.djangoproject.com/ticket/5622
    old new class IPAddressField(Field): 
    898898        kwargs['max_length'] = 15
    899899        Field.__init__(self, *args, **kwargs)
    900900
     901    def get_db_prep_value(self, value, connection, prepared=False):
     902        if not prepared:
     903            value = self.get_prep_value(value)
     904        return value or None
     905
    901906    def get_internal_type(self):
    902907        return "IPAddressField"
    903908
  • tests/regressiontests/model_fields/models.py

    old new class BooleanModel(models.Model): 
    6666    bfield = models.BooleanField()
    6767    string = models.CharField(max_length=10, default='abc')
    6868
     69class IP(models.Model):
     70    ip = models.IPAddressField(blank=True, null=True)
     71
    6972###############################################################################
    7073# ImageField
    7174
  • tests/regressiontests/model_fields/tests.py

    old new from django import forms 
    77from django.db import models
    88from django.core.exceptions import ValidationError
    99
    10 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel
     10from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, IP
    1111
    1212# If PIL available, do these tests.
    1313if Image:
    class SlugFieldTests(django.test.TestCas 
    214214        bs = BigS.objects.get(pk=bs.pk)
    215215        self.assertEqual(bs.s, 'slug'*50)
    216216
     217class IPFieldTests(django.test.TestCase):
     218    def test_ip_empty(self):
     219        """
     220        Check it's possible to use empty values in IP field (#5622).
     221        """
     222        ip = IP.objects.create(ip="")
     223        ip = IP.objects.get(pk=ip.pk)
     224        self.assertEqual(ip.ip, None)
    217225
    218226class ValidationTest(django.test.TestCase):
    219227    def test_charfield_raises_error_on_empty_string(self):
Back to Top