diff --git a/django/contrib/gis/management/commands/ogrinspect.py b/django/contrib/gis/management/commands/ogrinspect.py index 0256936dec..a507c1d0fc 100644 --- a/django/contrib/gis/management/commands/ogrinspect.py +++ b/django/contrib/gis/management/commands/ogrinspect.py @@ -124,10 +124,10 @@ class Command(BaseCommand): # This extra legwork is so that the dictionary definition comes # out in the same order as the fields in the model definition. rev_mapping = {v: k for k, v in mapping_dict.items()} - output.extend(['', '# Auto-generated `LayerMapping` dictionary for %s model' % model_name, + output.extend(['', '', '# Auto-generated `LayerMapping` dictionary for %s model' % model_name, '%s_mapping = {' % model_name.lower()]) - output.extend(" '%s' : '%s'," % ( + output.extend(" '%s': '%s'," % ( rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options['layer_key']].fields ) - output.extend([" '%s' : '%s'," % (options['geom_name'], mapping_dict[options['geom_name']]), '}']) + output.extend([" '%s': '%s'," % (options['geom_name'], mapping_dict[options['geom_name']]), '}']) return '\n'.join(output) + '\n' diff --git a/django/contrib/gis/utils/ogrinspect.py b/django/contrib/gis/utils/ogrinspect.py index 7432ab762c..9a091602fc 100644 --- a/django/contrib/gis/utils/ogrinspect.py +++ b/django/contrib/gis/utils/ogrinspect.py @@ -169,6 +169,7 @@ def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=Non yield '# This is an auto-generated Django model module created by ogrinspect.' yield 'from django.contrib.gis.db import models' yield '' + yield '' yield 'class %s(models.Model):' % model_name diff --git a/tests/gis_tests/inspectapp/tests.py b/tests/gis_tests/inspectapp/tests.py index 8a058d6a8e..6dec48cc73 100644 --- a/tests/gis_tests/inspectapp/tests.py +++ b/tests/gis_tests/inspectapp/tests.py @@ -70,6 +70,7 @@ class OGRInspectTest(TestCase): '# This is an auto-generated Django model module created by ogrinspect.', 'from django.contrib.gis.db import models', '', + '', 'class MyModel(models.Model):', ' float = models.FloatField()', ' int = models.{}()'.format('BigIntegerField' if GDAL_VERSION >= (2, 0) else 'FloatField'), @@ -96,6 +97,7 @@ class OGRInspectTest(TestCase): '# This is an auto-generated Django model module created by ogrinspect.', 'from django.contrib.gis.db import models', '', + '', 'class City(models.Model):', ' name = models.CharField(max_length=80)', ' population = models.{}()'.format('BigIntegerField' if GDAL_VERSION >= (2, 0) else 'FloatField'), @@ -126,6 +128,7 @@ class OGRInspectTest(TestCase): '# This is an auto-generated Django model module created by ogrinspect.\n' 'from django.contrib.gis.db import models\n' '\n' + '\n' 'class Measurement(models.Model):\n' )) @@ -148,6 +151,24 @@ class OGRInspectTest(TestCase): output = out.getvalue() self.assertIn('class City(models.Model):', output) + def test_mapping_option(self): + expected = ( + " geom = models.PointField(srid=-1)\n" + "\n" + "\n" + "# Auto-generated `LayerMapping` dictionary for City model\n" + "city_mapping = {\n" + " 'name': 'Name',\n" + " 'population': 'Population',\n" + " 'density': 'Density',\n" + " 'created': 'Created',\n" + " 'geom': 'POINT',\n" + "}\n") + shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp') + out = StringIO() + call_command('ogrinspect', shp_file, '--mapping', 'City', stdout=out) + self.assertIn(expected, out.getvalue()) + def get_ogr_db_string(): """