Ticket #5622: 5622.2.diff

File 5622.2.diff, 1.5 KB (added by msurdi, 6 years ago)

Fix and Tests

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

     
    185185        Used by the default implementations of ``get_db_prep_save``and
    186186        `get_db_prep_lookup```
    187187        """
    188         return value
     188        if self.empty_strings_allowed:
     189            return value
     190        else:
     191            return value or None
    189192
    190193    def get_db_prep_save(self, value):
    191194        "Returns field's value prepared for saving into a database."
  • tests/regressiontests/model_fields/models.py

     
    3939class BigS(models.Model):
    4040    s = models.SlugField(max_length=255)
    4141
     42class IPModel(models.Model):
     43    ip = models.IPAddressField(blank=True,null=True)
     44
    4245__test__ = {'API_TESTS':"""
    4346# Create a couple of Places.
    4447>>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34"))
     
    97100>>> bs = BigS.objects.get(pk=bs.pk)
    98101>>> bs.s == 'slug' * 50
    99102True
     103
     104# Regression test for #5622 -- Ensure that None is returned for empty values in IPAddressField
     105>>> ip_model = IPModel.objects.create(ip="")
     106>>> ip_model = IPModel.objects.get(pk=ip_model.pk)
     107>>> ip_model.ip == None
     108True
    100109"""}
Back to Top