From f4abba5200e3164003781698c0854383bbc210be Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 24 May 2012 09:57:22 +0200 Subject: [PATCH] Fixed #18367 -- Allowed LayerMapping to store strings in TextField. Thanks geoffhing@gmail.com for the report. --- django/contrib/gis/tests/layermap/models.py | 1 + django/contrib/gis/tests/layermap/tests.py | 9 +++++++++ django/contrib/gis/utils/layermapping.py | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/contrib/gis/tests/layermap/models.py b/django/contrib/gis/tests/layermap/models.py index 51213eb0b8..b58dccdf37 100644 --- a/django/contrib/gis/tests/layermap/models.py +++ b/django/contrib/gis/tests/layermap/models.py @@ -17,6 +17,7 @@ class CountyFeat(models.Model): class City(models.Model): name = models.CharField(max_length=25) + name_txt = models.TextField(default='') population = models.IntegerField() density = models.DecimalField(max_digits=7, decimal_places=1) dt = models.DateField() diff --git a/django/contrib/gis/tests/layermap/tests.py b/django/contrib/gis/tests/layermap/tests.py index 7e5f43c99d..85b8d0c8b5 100644 --- a/django/contrib/gis/tests/layermap/tests.py +++ b/django/contrib/gis/tests/layermap/tests.py @@ -272,3 +272,12 @@ class LayerMapTest(TestCase): lm = LayerMapping(Invalid, invalid_shp, invalid_mapping, source_srs=4326) lm.save(silent=True) + + def test_textfield(self): + "Tests that String content fits also in a TextField" + mapping = copy(city_mapping) + mapping['name_txt'] = 'Name' + lm = LayerMapping(City, city_shp, mapping) + lm.save(silent=True, strict=True) + self.assertEqual(City.objects.count(), 3) + self.assertEqual(City.objects.all().order_by('name_txt')[0].name_txt, "Houston") diff --git a/django/contrib/gis/utils/layermapping.py b/django/contrib/gis/utils/layermapping.py index ea3f3d7861..48d6c1b70e 100644 --- a/django/contrib/gis/utils/layermapping.py +++ b/django/contrib/gis/utils/layermapping.py @@ -332,7 +332,7 @@ class LayerMapping(object): val = unicode(ogr_field.value, self.encoding) else: val = ogr_field.value - if len(val) > model_field.max_length: + if model_field.max_length and len(val) > model_field.max_length: raise InvalidString('%s model field maximum string length is %s, given %s characters.' % (model_field.name, model_field.max_length, len(val))) elif isinstance(ogr_field, OFTReal) and isinstance(model_field, models.DecimalField):