Ticket #5622: empty-ip.diff

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

Updated to trunk (> Django 1.2.1)

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

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 65b60a0..e639d0f 100644
    a b class Field(object): 
    269269        """
    270270        if not prepared:
    271271            value = self.get_prep_value(value)
    272         return value
     272        if self.empty_strings_allowed:
     273            return value
     274        else:
     275            return value or None
    273276
    274277    def get_db_prep_save(self, value, connection):
    275278        "Returns field's value prepared for saving into a database."
  • tests/regressiontests/model_fields/models.py

    diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
    index 45cd223..035fcd2 100644
    a b 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

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    index 72a7d4d..7798622 100644
    a b 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.TestCase): 
    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