Fixed #18367 -- Allowed LayerMapping to store strings in TextField.

Thanks geoffhing@gmail.com for the report.
This commit is contained in:
Claude Paroz 2012-05-24 09:57:22 +02:00
parent f1ebcdc7c2
commit f4abba5200
3 changed files with 11 additions and 1 deletions

View File

@ -17,6 +17,7 @@ class CountyFeat(models.Model):
class City(models.Model): class City(models.Model):
name = models.CharField(max_length=25) name = models.CharField(max_length=25)
name_txt = models.TextField(default='')
population = models.IntegerField() population = models.IntegerField()
density = models.DecimalField(max_digits=7, decimal_places=1) density = models.DecimalField(max_digits=7, decimal_places=1)
dt = models.DateField() dt = models.DateField()

View File

@ -272,3 +272,12 @@ class LayerMapTest(TestCase):
lm = LayerMapping(Invalid, invalid_shp, invalid_mapping, lm = LayerMapping(Invalid, invalid_shp, invalid_mapping,
source_srs=4326) source_srs=4326)
lm.save(silent=True) 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")

View File

@ -332,7 +332,7 @@ class LayerMapping(object):
val = unicode(ogr_field.value, self.encoding) val = unicode(ogr_field.value, self.encoding)
else: else:
val = ogr_field.value 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.' % raise InvalidString('%s model field maximum string length is %s, given %s characters.' %
(model_field.name, model_field.max_length, len(val))) (model_field.name, model_field.max_length, len(val)))
elif isinstance(ogr_field, OFTReal) and isinstance(model_field, models.DecimalField): elif isinstance(ogr_field, OFTReal) and isinstance(model_field, models.DecimalField):